{"id":198,"date":"2018-10-10T08:44:00","date_gmt":"2018-10-10T00:44:00","guid":{"rendered":"https:\/\/yeslq.com\/201810198.html"},"modified":"2019-05-07T10:43:19","modified_gmt":"2019-05-07T02:43:19","slug":"the-linux-command-line-expansion-2","status":"publish","type":"post","link":"https:\/\/yeslq.com\/?p=198","title":{"rendered":"The Linux Command Line&#8212;Expansion"},"content":{"rendered":"<h1 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\n<i style=\"font-size: x-small;\"><a href=\"http:\/\/linuxcommand.org\/tlcl.php\" style=\"color: #002740;\">The Linux Command Line&nbsp;<\/a><\/i><span style=\"font-size: xx-small;\">by William Shotts<\/span><\/h1>\n<p><\/p>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nEach time you type a command line and press the enter key, bash performs several processes upon the text before it carries out your command. We have seen a couple of cases of how a simple character sequence, for example \u201c*\u201d, can have a lot of meaning to the shell. The process that makes this happen is called&nbsp;<i>expansion<\/i>. With expansion, you type something and it is expanded into something else before the shell acts upon it. To demonstrate what we mean by this, let&#8217;s take a look at the&nbsp;<a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/echoh.html\" style=\"color: #002740;\">echo<\/a>&nbsp;command.&nbsp;echo&nbsp;is a shell builtin that performs a very simple task. It prints out its text arguments on standard output:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo this is a test<br \/>\nthis is a test<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThat&#8217;s pretty straightforward. Any argument passed to&nbsp;echo&nbsp;gets displayed. Let&#8217;s try another example:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo *<br \/>\nDesktop Documents ls-output.txt Music Pictures Public Templates Videos<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nSo what just happened? Why didn&#8217;t&nbsp;echo&nbsp;print \u201c*\u201d? As you recall from our work with wildcards, the \u201c*\u201d character means match any characters in a filename, but what we didn&#8217;t see in our original discussion was how the shell does that. The simple answer is that the shell expands the \u201c*\u201d into something else (in this instance, the names of the files in the current working directory) before the&nbsp;echo&nbsp;command is executed. When the enter key is pressed, the shell automatically expands any qualifying characters on the command line before the command is carried out, so the&nbsp;echo&nbsp;command never saw the \u201c*\u201d, only its expanded result. Knowing this, we can see that&nbsp;echo&nbsp;behaved as expected.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\n<span style=\"font-size: small;\">Pathname Expansion<\/span><\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThe mechanism by which wildcards work is called&nbsp;<i>pathname expansion<\/i>. If we try some of the techniques that we employed in our earlier lessons, we will see that they are really expansions. Given a home directory that looks like this:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;ls<\/p>\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">Desktop\nls-output.txt\nDocuments Music\nPictures\nPublic\nTemplates\nVideos<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nwe could carry out the following expansions:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo D*<br \/>\nDesktop Documents<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nand:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo *s<br \/>\nDocuments Pictures Templates Videos<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nor even:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo [[:upper:]]*<br \/>\nDesktop Documents Music Pictures Public Templates Videos<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nand looking beyond our home directory:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo \/usr\/*\/share<br \/>\n\/usr\/kerberos\/share \/usr\/local\/share<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\n<span style=\"font-size: small;\">Tilde Expansion<\/span><\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nAs you may recall from our introduction to the&nbsp;cd&nbsp;command, the tilde character (\u201c~\u201d) has a special meaning. When used at the beginning of a word, it expands into the name of the home directory of the named user, or if no user is named, the home directory of the current user:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo ~<br \/>\n\/home\/me<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nIf user \u201cfoo\u201d has an account, then:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo ~foo<br \/>\n\/home\/foo<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\n<span style=\"font-size: small;\">Arithmetic Expansion<\/span><\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThe shell allows arithmetic to be performed by expansion. This allow us to use the shell prompt as a calculator:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo $((2 + 2))<br \/>\n4<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nArithmetic expansion uses the form:<\/div>\n<pre style=\"background-color: white; font-family: courier, lucidatypewriter, monospace;\"> $((expression))<\/pre>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nwhere expression is an arithmetic expression consisting of values and arithmetic operators.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nArithmetic expansion only supports integers (whole numbers, no decimals), but can perform quite a number of different operations.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nSpaces are not significant in arithmetic expressions and expressions may be nested. For example, to multiply five squared by three:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo $(($((5**2)) * 3))<br \/>\n75<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nSingle parentheses may be used to group multiple subexpressions. With this technique, we can rewrite the example above and get the same result using a single expansion instead of two:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo $(((5**2) * 3))<br \/>\n75<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nHere is an example using the division and remainder operators. Notice the effect of integer division:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo Five divided by two equals $((5\/2))<br \/>\nFive divided by two equals 2<br \/>\n[me@linuxbox me]$&nbsp;echo with $((5%2)) left over.<br \/>\nwith 1 left over.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\n<span style=\"font-size: small;\">Brace Expansion<\/span><\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nPerhaps the strangest expansion is called&nbsp;<i>brace expansion<\/i>. With it, you can create multiple text strings from a pattern containing braces. Here&#8217;s an example:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo Front-{A,B,C}-Back<br \/>\nFront-A-Back Front-B-Back Front-C-Back<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nPatterns to be brace expanded may contain a leading portion called a&nbsp;<i>preamble<\/i>&nbsp;and a trailing portion called a&nbsp;<i>postscript<\/i>. The brace expression itself may contain either a comma-separated list of strings, or a range of integers or single characters. The pattern may not contain embedded whitespace. Here is an example using a range of integers:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo Number_{1..5}<br \/>\nNumber_1 Number_2 Number_3 Number_4 Number_5<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nA range of letters in reverse order:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo {Z..A}<br \/>\nZ Y X W V U T S R Q P O N M L K J I H G F E D C B A<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nBrace expansions may be nested:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo a{A{1,2},B{3,4}}b<br \/>\naA1b aA2b aB3b aB4b<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nSo what is this good for? The most common application is to make lists of files or directories to be created. For example, if you were a photographer and had a large collection of images you wanted to organize into years and months, the first thing you might do is create a series of directories named in numeric \u201cYear-Month\u201d format. This way, the directory names will sort in chronological order. You could type out a complete list of directories, but that&#8217;s a lot of work and it&#8217;s error-prone too. Instead, you could do this:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;mkdir Photos<br \/>\n[me@linuxbox me]$&nbsp;cd Photos<br \/>\n[me@linuxbox Photos]$&nbsp;mkdir {2007..2009}-0{1..9} {2007..2009}-{10..12}<br \/>\n[me@linuxbox Photos]$&nbsp;ls<\/p>\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">2007-01 2007-07 2008-01 2008-07 2009-01 2009-07\n2007-02 2007-08 2008-02 2008-08 2009-02 2009-08\n2007-03 2007-09 2008-03 2008-09 2009-03 2009-09\n2007-04 2007-10 2008-04 2008-10 2009-04 2009-10\n2007-05 2007-11 2008-05 2008-11 2009-05 2009-11\n2007-06 2007-12 2008-06 2008-12 2009-06 2009-12<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nPretty slick!<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\n<span style=\"font-size: small;\">Parameter Expansion<\/span><\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nWe&#8217;re only going to touch briefly on&nbsp;<i>parameter expansion<\/i>&nbsp;in this lesson, but we&#8217;ll be covering it more later. It&#8217;s a feature that is more useful in shell scripts than directly on the command line. Many of its capabilities have to do with the system&#8217;s ability to store small chunks of data and to give each chunk a name. Many such chunks, more properly called&nbsp;<i>variables<\/i>, are available for your examination. For example, the variable named \u201cUSER\u201d contains your user name. To invoke parameter expansion and reveal the contents of USER you would do this:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo $USER<br \/>\nme<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nTo see a list of available variables, try this:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;printenv | less<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nYou may have noticed that with other types of expansion, if you mistype a pattern, the expansion will not take place and the echo command will simply display the mistyped pattern. With parameter expansion, if you misspell the name of a variable, the expansion will still take place, but will result in an empty string:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo $SUER<br \/>\n[me@linuxbox ~]$<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\n<span style=\"font-size: small;\">Command Substitution<\/span><\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\n<i>Command substitution<\/i>&nbsp;allows us to use the output of a command as an expansion:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo $(ls)<br \/>\nDesktop Documents ls-output.txt Music Pictures Public Templates Videos<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nOne of my favorites goes something like this:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;ls -l $(which cp)<br \/>\n-rwxr-xr-x 1 root root 71516 2007-12-05 08:58 \/bin\/cp<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nHere we passed the results of which&nbsp;cp&nbsp;as an argument to the&nbsp;ls&nbsp;command, thereby getting the listing of of the&nbsp;cp&nbsp;program without having to know its full pathname. We are not limited to just simple commands. Entire pipelines can be used (only partial output shown):<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;file $(ls \/usr\/bin\/* | grep bin\/zip)<\/p>\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">\/usr\/bin\/bunzip2:\n\/usr\/bin\/zip:      ELF 32-bit LSB executable, Intel 80386, version 1 \n(SYSV), dynamically linked (uses shared libs), for GNU\/Linux 2.6.15, stripped\n\/usr\/bin\/zipcloak: ELF 32-bit LSB executable, Intel 80386, version 1\n(SYSV), dynamically linked (uses shared libs), for GNU\/Linux 2.6.15, stripped\n\/usr\/bin\/zipgrep:  POSIX shell script text executable\n\/usr\/bin\/zipinfo:  ELF 32-bit LSB executable, Intel 80386, version 1\n(SYSV), dynamically linked (uses shared libs), for GNU\/Linux 2.6.15, stripped\n\/usr\/bin\/zipnote:  ELF 32-bit LSB executable, Intel 80386, version 1\n(SYSV), dynamically linked (uses shared libs), for GNU\/Linux 2.6.15, stripped\n\/usr\/bin\/zipsplit: ELF 32-bit LSB executable, Intel 80386, version 1\n(SYSV), dynamically linked (uses shared libs), for GNU\/Linux 2.6.15, stripped<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nIn this example, the results of the pipeline became the argument list of the file command. There is an alternate syntax for command substitution in older shell programs which is also supported in&nbsp;bash. It uses back-quotes instead of the dollar sign and parentheses:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;ls -l `which cp`<br \/>\n-rwxr-xr-x 1 root root 71516 2007-12-05 08:58 \/bin\/cp<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\n<span style=\"font-size: small;\">Quoting<\/span><\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nNow that we&#8217;ve seen how many ways the shell can perform expansions, it&#8217;s time to learn how we can control it. Take for example:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo this is a&nbsp;&nbsp;&nbsp;&nbsp; test<br \/>\nthis is a test<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nor:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;[me@linuxbox ~]$ echo The total is $100.00<br \/>\nThe total is 00.00<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nIn the first example, word-splitting by the shell removed extra whitespace from the echo command&#8217;s list of arguments. In the second example, parameter expansion substituted an empty string for the value of \u201c$1\u201d because it was an undefined variable. The shell provides a mechanism called quoting to selectively suppress unwanted expansions.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\n<span style=\"font-size: small;\">Double Quotes<\/span><\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThe first type of quoting we will look at is double quotes. If you place text inside double quotes, all the special characters used by the shell lose their special meaning and are treated as ordinary characters. The exceptions are \u201c$\u201d, \u201c\u201d (backslash), and \u201c`\u201d (back- quote). This means that word-splitting, pathname expansion, tilde expansion, and brace expansion are suppressed, but parameter expansion, arithmetic expansion, and command substitution are still carried out. Using double quotes, we can cope with filenames containing embedded spaces. Say you were the unfortunate victim of a file called two words.txt. If you tried to use this on the command line, word-splitting would cause this to be treated as two separate arguments rather than the desired single argument:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;ls -l two words.txt<\/p>\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">ls: cannot access two: No such file or directory\nls: cannot access words.txt: No such file or directory<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nBy using double quotes, you can stop the word-splitting and get the desired result; further, you can even repair the damage:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;ls -l &#8220;two words.txt&#8221;<br \/>\n-rw-rw-r&#8211; 1 me me 18 2008-02-20 13:03 two words.txt<br \/>\n[me@linuxbox me]$&nbsp;mv &#8220;two words.txt&#8221; two_words.txt<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThere! Now we don&#8217;t have to keep typing those pesky double quotes. Remember, parameter expansion, arithmetic expansion, and command substitution still take place within double quotes:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo &#8220;$USER $((2+2)) $(cal)&#8221;<\/p>\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">me 4\nFebruary 2008\nSu Mo Tu We Th Fr Sa\n                1  2\n 3  4  5  6  7  8  9\n10 11 12 13 14 15 16\n17 18 19 20 21 22 23\n24 25 26 27 28 29<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nWe should take a moment to look at the effect of double quotes on command substitution. First let&#8217;s look a little deeper at how word splitting works. In our earlier example, we saw how word-splitting appears to remove extra spaces in our text:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo this is a &nbsp;&nbsp;&nbsp;&nbsp;test<br \/>\nthis is a test<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nBy default, word-splitting looks for the presence of spaces, tabs, and newlines (linefeed characters) and treats them as delimiters between words. This means that unquoted spaces, tabs, and newlines are not considered to be part of the text. They only serve as separators. Since they separate the words into different arguments, our example command line contains a command followed by four distinct arguments. If we add double quotes:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo &#8220;this is a &nbsp;&nbsp;&nbsp;&nbsp;test&#8221;<br \/>\nthis is a &nbsp;&nbsp;&nbsp;&nbsp;test<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nword-splitting is suppressed and the embedded spaces are not treated as delimiters, rather they become part of the argument. Once the double quotes are added, our command line contains a command followed by a single argument. The fact that newlines are considered delimiters by the word-splitting mechanism causes an interesting, albeit subtle, effect on command substitution. Consider the following:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo $(cal)<br \/>\nFebruary 2008 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29<br \/>\n[me@linuxbox me]$&nbsp;echo &#8220;$(cal)&#8221;<\/p>\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">February 2008\nSu Mo Tu We Th Fr Sa\n                1  2\n 3  4  5  6  7  8  9\n10 11 12 13 14 15 16\n17 18 19 20 21 22 23\n24 25 26 27 28 29\n<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nIn the first instance, the unquoted command substitution resulted in a command line containing thirty-eight arguments. In the second, a command line with one argument that includes the embedded spaces and newlines.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\n<span style=\"font-size: small;\">Single Quotes<\/span><\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nIf you need to suppress all expansions, you use single quotes. Here is a comparison of unquoted, double quotes, and single quotes:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo text ~\/*.txt {a,b} $(echo foo) $((2+2)) $USER<br \/>\ntext \/home\/me\/ls-output.txt a b foo 4 me<br \/>\n[me@linuxbox me]$&nbsp;echo &#8220;text ~\/*.txt {a,b} $(echo foo) $((2+2)) $USER&#8221;<br \/>\ntext ~\/*.txt {a,b} foo 4 me<br \/>\n[me@linuxbox me]$&nbsp;echo &#8216;text ~\/*.txt {a,b} $(echo foo) $((2+2)) $USER&#8217;<br \/>\ntext ~\/*.txt {a,b} $(echo foo) $((2+2)) $USER<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nAs you can see, with each succeeding level of quoting, more and more of the expansions are suppressed.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\n<span style=\"font-size: small;\">Escaping Characters<\/span><\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nSometimes you only want to quote a single character. To do this, you can precede a character with a backslash, which in this context is called the&nbsp;<i>escape character<\/i>. Often this is done inside double quotes to selectively prevent an expansion:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo &#8220;The balance for user $USER is: $5.00&#8221;<br \/>\nThe balance for user me is: $5.00<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nIt is also common to use escaping to eliminate the special meaning of a character in a filename. For example, it is possible to use characters in filenames that normally have special meaning to the shell. These would include \u201c$\u201d, \u201c!\u201d, \u201c&amp;\u201d, \u201c \u201c, and others. To include a special character in a filename you can to this:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;mv bad&amp;filename good_filename<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nTo allow a backslash character to appear, escape it by typing \u201c\u201d. Note that within single quotes, the backslash loses its special meaning and is treated as an ordinary character.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\n<span style=\"font-size: small;\">More Backslash Tricks<\/span><\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nIf you look at the&nbsp;man&nbsp;pages for any program written by the&nbsp;<a href=\"http:\/\/www.gnu.org\/\" style=\"color: #002740;\">GNU project<\/a>, you will notice that in addition to command line options consisting of a dash and a single letter, there are also long option names that begin with two dashes. For example, the following are equivalent:<\/div>\n<div class=\"codeexample\" style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">ls -r\nls --reverse\n       \n<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nWhy do they support both? The short form is for lazy typists on the command line and the long form is mostly for scripts though some options may only be long form. I sometimes use obscure options, and I find the long form useful if I have to review a script again months after I wrote it. Seeing the long form helps me understand what the option does, saving me a trip to the&nbsp;man&nbsp;page. A little more typing now, a lot less work later. Laziness is maintained.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nAs you might suspect, using the long form options can make a single command line very long. To combat this problem, you can use a backslash to get the shell to ignore a newline character like this:<\/div>\n<div class=\"codeexample\" style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">ls -l \n   --reverse \n   --human-readable \n   --full-time\n       \n<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nUsing the backslash in this way allows us to embed newlines in our command. Note that for this trick to work, the newline must be typed immediately after the backslash. If you put a space after the backslash, the space will be ignored, not the newline. Backslashes are also used to insert special characters into our text. These are called&nbsp;<i>backslash escape characters<\/i>. Here are the common ones:<\/div>\n<table border=\"\" cellpadding=\"8\" style=\"background-color: white; color: black; font-family: verdana, arial, helvetica, sans-serif;\">\n<tbody>\n<tr>\n<td valign=\"top\"><strong>Escape Character<\/strong><\/td>\n<td valign=\"top\"><strong>Name<\/strong><\/td>\n<td valign=\"top\"><strong>Possible Uses<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">n<\/td>\n<td valign=\"top\">newline<\/td>\n<td valign=\"top\">Adding blank lines to text<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">t<\/td>\n<td valign=\"top\">tab<\/td>\n<td valign=\"top\">Inserting horizontal tabs to text<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">a<\/td>\n<td valign=\"top\">alert<\/td>\n<td valign=\"top\">Makes your terminal beep<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><\/td>\n<td valign=\"top\">backslash<\/td>\n<td valign=\"top\">Inserts a backslash<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">f<\/td>\n<td valign=\"top\">formfeed<\/td>\n<td valign=\"top\">Sending this to your printer ejects the page<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThe use of the backslash escape characters is very common. This idea first appeared in the C programming language. Today, the shell, C++, perl, python, awk, tcl, and many other programming languages use this concept. Using the&nbsp;echo&nbsp;command with the -e option will allow us to demonstrate:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo -e &#8220;Inserting several blank linesnnn&#8221;<br \/>\nInserting several blank lines<\/p>\n<p>[me@linuxbox me]$&nbsp;echo -e &#8220;Wordstseparatedtbythorizontalttabs.&#8221;<\/p>\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">Words separated   by  horizontal  tabs\n<\/pre>\n<p>[me@linuxbox me]$&nbsp;echo -e &#8220;aMy computer went &#8220;beep&#8221;.&#8221;<\/p>\n<p>My computer went &#8220;beep&#8221;.<\/p>\n<p>[me@linuxbox me]$&nbsp;echo -e &#8220;DEL C:WIN2KLEGACY_OS.EXE&#8221;<\/p>\n<p>DEL C:WIN2KLEGACY_OS.EXE<\/p><\/div>\n<div class=\"pagenav\" style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; padding: 1em;\">\n<div class=\"right\" style=\"text-align: right;\">\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Linux Command Line&nbsp;by William Shotts Each time &#8230; <a title=\"The Linux Command Line&#8212;Expansion\" class=\"read-more\" href=\"https:\/\/yeslq.com\/?p=198\" aria-label=\"\u9605\u8bfb The Linux Command Line&#8212;Expansion\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[51,52,53],"class_list":["post-198","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-linux","tag-shell","tag-the-linux-command-line"],"_links":{"self":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=198"}],"version-history":[{"count":1,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/198\/revisions"}],"predecessor-version":[{"id":305,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/198\/revisions\/305"}],"wp:attachment":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}