Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading