how to define a variable in .bashrc so i can use it as path
Hi ( sorry for my bad English )
I just learned how to permanently set a key for a specific value using alias:
.bashrc
alias please='sudo'
alias go='cd'
alias destroy='rm -rf'
And it works perfectly fine. but then I thought myself how fun would it be if I could store my favorite paths (like ~/Music) in .bashrc for easier use. so I did this:
alias please='sudo'
alias go='cd'
alias destroy='rm -rf'
alias home='~'
alias work='~/Workstation'
alias back='..'
but It didn't work.
I also tried defining a variables like this:
back='..'
and it also didn't work.
I know I can do alias gowork='cd ~/Workstation'
but I want to be able to use the path I stored in many different commands like this:
destroy work
and I want to be able to do things like this:
go back/Pictures
Any help would be strongly appreciated thank you guys!
An "alias" is an abbreviation for a shell command. Your definition alias home='~'
does not work because it does not specify a valid command:
~$ ~
bash: /home/vanadium: Is a directory
This approach, therefore, is not suited to allow you to replace a full pathname by a shorter name for it that you can use in commands.
One way is to define variables instead. There is probably no need to define shortcuts for your home directory and for the previous folders: the build-in abbreviations, ~
and ..
, respectively, are as short as it can get: I advise you to just adopt these.
For other paths, you can define environmental variables, which, similar as aliasses, can be made permanent by including them in .bashrc
:
export work=~/Workstation
which then can be used in a command as
cd $work
and which will work with your other aliases, e.g.
destroy $work
Notes if dealing with pathnames with spaces:
• If the pathname defined in the variable contains spaces, you will need to quote the variable as in
cd "$work"
• If you define a variable with spaces, you need to keep symbols that are expanded by bash, e.g. ~
, unquoted, as in
export work=~"/Pathname with spaces"