Bash Expansion: Difference between revisions

From Psygen Wiki
Jump to navigation Jump to search
(Created page with "Bash brace expansion "autofills" some commands for you. == Create Multiple files == <code>touch file{1,2,3,4}.txt</code> - Expands to: <pre> file1.txt file2.txt file3.t...")
 
 
(4 intermediate revisions by the same user not shown)
Line 29: Line 29:
  file6.txt
  file6.txt
</pre>
</pre>
== Save Typing ==
<code>/a/long/path/{foo,bar}</code> expands to: <code>/a/long/path/foo /a/long/path/bar</code> (Handy for copy commands...)
'''Make a backup copy of a file:'''<br />
<code>cp -a /home/user/public_html/test.txt{,.bak}</code> - is the same as typing: <code>cp -a /home/user/public_html/test.txt /home/user/public_html/test.txt.bak</code>
== References ==
<ul>
  <li>[http://www.devopsservice.com/bash-brace-expansion-tutorial devopservice tutorial]</li>
</ul>

Latest revision as of 03:54, 15 July 2017

Bash brace expansion "autofills" some commands for you.


Create Multiple files

touch file{1,2,3,4}.txt - Expands to:

 file1.txt
 file2.txt
 file3.txt
 file4.txt


So, we can use a comma to separate the stuff we want to expand. Can we do ranges, I don't want to type all that?

We sure can!!


touch file{1..6}.txt - Expands to:

 file1.txt
 file2.txt
 file3.txt
 file4.txt
 file5.txt
 file6.txt

Save Typing

/a/long/path/{foo,bar} expands to: /a/long/path/foo /a/long/path/bar (Handy for copy commands...)


Make a backup copy of a file:
cp -a /home/user/public_html/test.txt{,.bak} - is the same as typing: cp -a /home/user/public_html/test.txt /home/user/public_html/test.txt.bak

References