Is there a way to echo current directory with ~
replacing home directory?
Example:~/inbox
instead of /home/john/inbox
Home directory should not be hardcoded.
There are $PWD
and $HOME
variables.
A built-in bash tool would be nice.
Now when you open Git Bash and type pwd you should see the new folder is automatically where you are. To change your current directory to the users home directory type cd ~ . The ~ represents the home directory which is set in the %HOME% environment variable.
To change directories, use the command cd followed by the name of the directory (e.g. cd downloads ). Then, you can print your current working directory again to check the new path.
How do I get the current working directory under Bash or Ksh shell running on Linux or Unix like operating systems? [a] OLDPWD The previous working directory as set by the cd command. [b] PWD The current working directory as set by the cd command. pwd command – Print the name of the current working directory.
echo "${PWD/#$HOME/\~}"
This substitutes $HOME
with ~
. The #
is like ^
in a regex: it anchors the match to the beginning of the string. The \~
replaces $HOME
with a literal tilde; if we didn't use the \
escape, the ~
would re-expand to $HOME
and effectively do nothing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With