- Create a variable
city=Boston
- Display the value of this variable
echo $city
- Define another variable using single quotes
capitol='$city is the capitol of Massachusetts'
- Display the value of this variable
echo $capitol
The variable city is not evaluated because it was enclosed in
single quotes when defined.
- Redefine city using double quotes
capitol="$city is the capitol of Massachusetts"
- Display this redefined variable
echo $capitol
The double quotes permitted the shell to evaluate city when
assigning a value to capitol.
- Make sure you are in your home directory
cd
- Display the value of PWD
echo $PWD
PWD has the address of your home directory.
- Go to my home directory
cd ~ghoffman
- Display the value of PWD
echo $PWD
PWD is now set to the address of my home directory.
- Create an alias using this variable in double quotes
alias new_pwd="echo $PWD"
- Use this alias in your current location
new_pwd
It displays the address of my home directory.
- Return to your home directory
cd
- Make sure you are in your home directory
pwd
- Use the alias in this new location
new_pwd
The alias again returns the address of my home directory.
Since you created the alias in my home directory, and used double
quotes, the value of PWD at the time of definition was used in creating
the alias.
But you want alias to use the value of PWD at the time the alias is used.
- Redefine the alias using single quotes
alias new_pwd='echo $PWD'
- Use the newly redefined alias
new_pwd
It displays the address of your home directory.
- Go to my home directory
cd ~ghoffman
- Use the redefined alias again
new_pwd
It displays the address of my home directory.
Since you defined the alias using single quotes, PWD was
not evaluated when it was defined, but instead, when it
is used.