Shell Expansions
While working in a shell, sets or ranges of information are often repeated. Shell expansion helps generate a large number of shell tokens using compact syntaxes. The expansion is performed on the command line after the command is split into tokens. Of the many expansions available, the path name, file name, and brace expansions are explained ahead.
Path Name Expansion
The pathname expansion simplifies location changes within the directory hierarchy. The pathname expansion includes:
– The tilde (~) character, which represents the absolute pathname of the home directory of the current user
– The tilde (~) character with a username, which represents the home directory of the specified user
– The dash (-) character, which represents the previous working directory
Consider the following examples for each of the expansion character:
1. Change directories to dir1 by using the tilde (~) character.
$ cd ~/dir1 $ pwd /home/user/dir1/
2. Change directories to the user home directory using the tilde (~) character followed by a username.
$ cd ~user $ pwd /home/user
3. Switch between the user and tmp directories using the – expansion character.
$ cd $ pwd /home/user
$ cd /tmp $ pwd /tmp
$ cd - /export/home/user1 $ cd - /tmp
File Name Expansion
The file name expansions are:
- The asterisk (*) character
- The question mark (?) character
- The square bracket ([]) characters
1. Asterisk (*) Character
The asterisk (*) expansion character is also called the wildcard character and represents zero or more characters, except the leading period (.) of a hidden file. For example, list all files and directories that start with the letter f followed by zero or more other characters.
$ ls f* file.1 file.2 file.3 file4
Another example is to list all files and directories that end with the number 3, preceded by zero or more characters.
$ ls *3 file.3 file3
2. Question Mark (?) Character
The question mark (?) character is also called a wildcard character and represents any single character except the leading period (.) of a hidden file. For example, list all files and directories that start with the string dir and followed by one other character.
$ ls dir? dir1: coffees fruit trees dir2: beans notes recipes
If no files match an entry using the question mark (?) character, an error message appears.
$ ls z? z?: No such file or directory
3. Square Bracket ([]) Characters
The square bracket ([]) characters represent a set or range of characters for a single character position.
– A set of characters is any number of specific characters, for example, [acb]. The characters in a set do not necessarily have to be in any order. For example, [abc] is the same as [cab].
– A range of characters is a series of ordered characters.
- A range lists the first character followed by a hyphen (-) and then the last character, for example, [a–z] or [0–9].
- When specifying a range, arrange the characters in the order that you want them to appear in the output.
- For example, use [A–Z] or [a–z] to search for any uppercase or lowercase alphabetical character, respectively.
For example, list all files and directories that start with the letters a through f.
$ ls [a-f]* brands dante_1 file.1 file2 file4 celery feathers file1 file.3 fruit dante feathers_6 file.2 file3 fruit2
For example, list all files and directories that start with the letters f or p.
$ ls [fp]* perm file.1 file.2 file.3 file4 fruit2 feathers_6 file1 file2 file3 fruit
The Brace Expansion
The brace {} expansion is a mechanism by which arbitrary strings may be generated. Patterns to be brace expanded take the form of an optional preamble, followed by either a series of comma-separated strings or a sequence expression between a pair of braces, followed by an optional postscript. The preamble “a” is prefixed to each string contained within the braces, and the postscript “e” is then appended to each resulting string, expanding left to right.
$ echo a{d,c,b}e ade ace abe