Quantcast
Channel: Expansion of a shell variable and effect of glob and split on it - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 3

Answer by slm for Expansion of a shell variable and effect of glob and split on it

$
0
0

glob/split

I'll take the glob/split first. @Stephane's answer you linked to is using those terms in a general sense. They are not actual commands or anything like that, just pseudo operations.

The split("$test") would split the contents of "$test" up into an "array" of elements.

The glob(...) would then take care to expand any of these elements that contain shell globbing characters such as * or ranges [1-2].

Example

Say our string $test is as follows.

$ test="afile[1-2] afile[3-5]"

Also let's say we have a directory with some files in it.

$ ls -1
afile1
afile2
afile3
afile4
afile5

Now if we attempt to echo it without quotes you should notice that our string got split up on spaces, and then any globbing characters got expanded.

$ echo $test
afile1 afile2 afile3 afile4 afile5

However if we were to quote the variable when we passed it as an argument to echo we'd get the original literal string.

$ echo "$test"
afile[1-2] afile[3-5]

variable expansion

The term variable expansion is meant to cover the basic operation that the shell is performing as part of it's basic operations. The shell is responsible for parsing input and then executing this input once it's deemed syntactically correct.

In our previous example. When the variable $test was presented to echo unquoted we were telling the shell to go ahead and split those arguments up and then glob them.

When it was quoted, we were essentially disabling that feature with what ever variable(s) we wrapped with double quotes.

Example

Here are some additional examples of globbing and splitting.

glob/splitting happens automatically

$ echo file{1..3}
file1 file2 file3

$ echo file{1..3} dir{a..b}
file1 file2 file3 dira dirb

$ echo dir{z..w} file{A..D}
dirz diry dirx dirw fileA fileB fileC fileD

$ echo dir{z..w} file{A..B} fileC
dirz diry dirx dirw fileA fileB fileC

glob/splitting disabled via double quoting

$ echo "dir{z..w} file{A..B} fileC"
dir{z..w} file{A..B} fileC

$ echo "dir{z..w} file{A..B}"
dir{z..w} file{A..B}

Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>