Score:3

how to define a variable in .bashrc so i can use it as path

sh flag

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!

Score:9
cn flag

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"
YoloWex avatar
sh flag
It seems right but it returns ```frnr@frnr-System-Product-Name:~$ go $work bash: cd: ~/Workstation: No such file or directory ```
vanadium avatar
cn flag
@bac0n thanks, yes, the quotes prevent "~" from being expanded while setting the variable: ~ then becomes literal part of the variable, and is not expanded when retrieving its value in a command. Corrected!
YoloWex avatar
sh flag
dropping the quotes worked!! thanks a lot ! you guy's are life savors. @vanadium
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.