Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

9
  • This answer stopped me from using sed with the pwd command to avoid defining a new variable each time my custom $PS1 runs. Does Bash provide a more general way than magic variables to use the output of a command for string replacement? As for your code, I had to escape the ~ to keep Bash from expanding it into $HOME. Also, what does the # in your command do? Commented May 26, 2015 at 0:05
  • 2
    @MarkHaferkamp See this from the "further reading recommended" link. About "escaping the ~": notice how I quoted stuff. Remember to always quote stuff! And this doesn't just work for magic variables: any variable is capable of substitutions, getting string length, and more, within bash. Congrats on trying to your $PS1 fast: you may also be interested in $PROMPT_COMMAND if you are more comfortable in another programming language and want to code a compiled prompt. Commented May 27, 2015 at 19:33
  • The "further reading" link explains the "#". On Bash 4.3.30, echo "${PWD/#$HOME/~}" doesn't replace my $HOME with ~. Replacing ~ with \~ or '~' works. Any of these work on Bash 4.2.53 on another distro. Can you please update your post to quote or escape the ~ for better compatibility? What I meant by my "magic variables" question was: Can I use Bash's variable substitution on, e.g., the output of uname without saving it as a variable first? As for my personal $PROMPT_COMMAND, it's complicated. Commented May 30, 2015 at 7:46
  • @MarkHaferkamp Whoa, you're totally right, my bad. Will update the answer now. Commented May 31, 2015 at 5:45
  • 3
    @MarkHaferkamp Bash and its obscure pitfalls... :P Commented Jun 10, 2015 at 13:28