The Bash shell provides some variables that are prefixed with ‘~’ (named tilde) which is called Tilde Expansions.
They are synonyms for contents of other variables within your shell.
Tilde expansion is the process of converting these abbreviations to the directory names that they stand for. In this article, let us review the tilde expansion feature with the examples.
Tilde expansion applies to the ‘~’ plus characters includes +, – and N (which is integer) up to whitespace or a slash.
The tilde expansion is used to expand to several specific pathnames:
Tilde expansion provides a way to expand the home directory of the current user or the home directory of the given user name.
Syntax ~ Expand to the variable $HOME or home directory of the current user ~USER Expand to the home directory of the given username
Tilde(~) as a separate word, expands to $HOME if it is defined, if $HOME is not defined, then it expands with the home directory of the current user.
Now the value of the HOME variable is /home/oracle so cd ~ changed the current directory to the value of $HOME.
## Logging into a oracle user, whose home directory is /home/oracle # su oracle [tmp]$ pwd /tmp [tmp]$ echo $HOME /home/oracle [tmp]$ cd ~ [~]$ pwd /home/oracle
HOME is changed to /sbin, and cd ~ uses only $HOME not the home directory of the user. After unset the value of $HOME, cd ~ changed the directory to the value of the home directory set for the oracle user in /etc/passwd. For Tilde expansion, HOME overrides the real home directory.
[~]$ export HOME=/sbin [oracle]$ cd ~ [~]$ pwd /sbin [~]$ unset HOME [sbin]$ cd ~ [oracle]$ pwd /home/oracle
The following script takes the backup of a log file that has the current date in the name. It also logs the starting time and end time into the file called backup.log in the home directory of the oracle user.
#! /bin/bash echo "Initiating the backup at `date`" >> ~oracle/backup.log da=`date +%F` cp $da.log{,.bak} echo "END BACKUP at `date`" >> ~oracle/backup.log $ ls -l /home/oracle/ total 8 -rw-r--r-- 1 root root 99 Jun 4 14:23 backup.log
If the given user name does not exist then it doesn’t expand to something. In the following example, there is no user called ora. So, ~ora will not expand to /home/ora.
$ echo ~ora ~ora
Refer to our earlier article to understand how to perform brace expansion in bash. i.e how to use { } in bash.
Tilde with + and – are used for representing the working directories.
The following example will compare the file in the current directory and previous working directory.
$ cat comp.sh #! /bin/bash set -x cd /var/opt/gg if [ -f gg.c ] then echo "File1 exists" fi cd /var/opt1/gg if [ -f gg.c ] then echo "File2 exists" cmp ~+/gg.c ~-/gg.c fi $ ./comp.sh + cd /var/opt/gg + '[' -f gg.c ']' + echo 'File1 exists' File1 exists + cd /var/opt1/gg + '[' -f gg.c ']' + echo 'File2 exists' File2 exists + cmp /var/opt1/gg/gg.c /var/opt/gg/gg.c cmp: EOF on /var/opt1/gg/gg.c $
In the above execution:
This article is part of the on-going Bash Tutorial series.
Each bash process contains a stack object that can be used to keep track of directories a script did visit while it is processing data of directory contents.
It is a very simple mechanism to be able to reference directories or to change back to directories one did visit before. Tilde expansion also provides expansion to the directories in the directory stack.
Review our earlier article to understand how to use dirs, pushd and popd commands to manipulate directory stack.
In the following example, directory stack has 4 directories. ~+2 gives you the directory path available in the second position from left starting with zero.
$ dirs -v 0 /sbin 1 /var/opt/midas 2 /var/opt/GG/bin 3 /root $ cd ~+2 $ pwd /var/opt/GG/bin
But top of the stack (zero position) will always have the current directory. So after the above execution, following is shown in the directory stack.
$ dirs -v 0 /var/opt/GG/bin 1 /var/opt/midas 2 /var/opt/GG/bin 3 /root
Following is similar to the above example. But, will consider the directories from the bottom of the stack because of the ~-.
$ dirs -v 0 /var/opt/GG/bin 1 /var/opt/midas 2 /var/opt/GG/bin 3 /root $ cd ~-2 $ pwd /var/opt/midas $ dirs -v 0 /var/opt/midas 1 /var/opt/midas 2 /var/opt/GG/bin 3 /root