{"id":90700,"date":"2019-03-26T14:00:50","date_gmt":"2019-03-26T14:00:50","guid":{"rendered":"http:\/\/www.sickgaming.net\/blog\/2019\/03\/26\/using-square-brackets-in-bash-part-1\/"},"modified":"2019-03-26T14:00:50","modified_gmt":"2019-03-26T14:00:50","slug":"using-square-brackets-in-bash-part-1","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2019\/03\/26\/using-square-brackets-in-bash-part-1\/","title":{"rendered":"Using Square Brackets in Bash: Part 1"},"content":{"rendered":"<div><img decoding=\"async\" src=\"http:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/03\/using-square-brackets-in-bash-part-1.jpg\" class=\"ff-og-image-inserted\"><\/div>\n<p>After taking a look at <a href=\"https:\/\/www.linux.com\/blog\/learn\/2019\/2\/all-about-curly-braces-bash\">how curly braces (<code>{}<\/code>) work on the command line<\/a>, now it\u2019s time to tackle brackets (<code>[]<\/code>) and see how they are used in different contexts.<\/p>\n<h3>Globbing<\/h3>\n<p>The first and easiest use of square brackets is in <em>globbing<\/em>.&nbsp;You have probably used globbing before without knowing it. Think of all the times you have listed files of a certain type, say, you wanted to list JPEGs, but not PNGs:<\/p>\n<pre>\nls *.jpg<\/pre>\n<p>Using wildcards to get all the results that fit a certain pattern is precisely what we call globbing.<\/p>\n<p>In the example above, the asterisk means &#8220;<i>zero or more characters<\/i>&#8220;. There is another globbing wildcard, <code>?<\/code>, which means &#8220;<i>exactly one character<\/i>&#8220;, so, while<\/p>\n<pre> ls d*k*\n<\/pre>\n<p>will list files called <i>darkly<\/i> and <i>ducky<\/i> (and <i>dark<\/i> and <i>duck<\/i> &#8212; remember <code>*<\/code> can also be zero characters),<\/p>\n<pre> ls d*k? <\/pre>\n<p>will not list <i>darkly<\/i> (or <i>dark<\/i> or <i>duck<\/i>), but it will list <i>ducky<\/i>.<\/p>\n<p>Square brackets are used in globbing for sets of characters. To see what this means, make directory in which to carry out tests, <code>cd<\/code> into it and create a bunch of files like this:<\/p>\n<pre> touch file0{0..9}{0..9} <\/pre>\n<p>(If you don&#8217;t know why that works, <a href=\"https:\/\/www.linux.com\/blog\/learn\/2019\/2\/all-about-curly-braces-bash\">take a look at the last installment that explains curly braces <code>{}<\/code><\/a>).<\/p>\n<p>This will create files <i>file000<\/i>, <i>file001<\/i>, <i>file002<\/i>, etc., through <i>file097<\/i>, <i>file098<\/i> and <i>file099<\/i>.<\/p>\n<p>Then, to list the files in the 70s and 80s, you can do this:<\/p>\n<pre> ls file0[78]? <\/pre>\n<p>To list <i>file022<\/i>, <i>file027<\/i>, <i>file028<\/i>, <i>file052<\/i>, <i>file057<\/i>, <i>file058<\/i>, <i>file092<\/i>, <i>file097<\/i>, and <i>file98<\/i> you can do this:<\/p>\n<pre> ls file0[259][278] <\/pre>\n<p>Of course, you can use globbing (and square brackets for sets) for more than just <code>ls<\/code>. You can use globbing with any other tool for listing, removing, moving, or copying files, although the last two may require a bit of lateral thinking.<\/p>\n<p>Let&#8217;s say you want to create duplicates of files <i>file010<\/i> through <i>file029<\/i> and call the copies <i>archive010<\/i>, <i>archive011<\/i>, <i>archive012<\/i>, etc..<\/p>\n<p>You can&#8217;t do:<\/p>\n<pre> cp file0[12]? archive0[12]? <\/pre>\n<p>Because globbing is for matching against existing files and directories and the <i>archive&#8230;<\/i> files don&#8217;t exist yet.<\/p>\n<p>Doing this:<\/p>\n<pre> cp file0[12]? archive0[1..2][0..9] <\/pre>\n<p>won&#8217;t work either, because <code>cp<\/code> doesn&#8217;t let you copy many files to other many new files. Copying many files only works if you are copying them to a directory, so this:<\/p>\n<pre> mkdir archive cp file0[12]? archive <\/pre>\n<p>would work, but it would copy the files, using their same names, into a directory called <i>archive\/<\/i>. This is not what you set out to do.<\/p>\n<p>However, if you look back at <a href=\"https:\/\/www.linux.com\/blog\/learn\/2019\/2\/all-about-curly-braces-bash\">the article on curly braces (<code>{}<\/code>)<\/a>, you will remember how you can use <code>%<\/code> to lop off the end of a string contained in a variable.<\/p>\n<p>Of course, there is a way you can also lop of the beginning of string contained in a variable. Instead of <code>%<\/code>, you use <code>#<\/code>.<\/p>\n<p>For practice, you can try this:<\/p>\n<pre> myvar=\"Hello World\" echo Goodbye Cruel ${myvar#Hello} <\/pre>\n<p>It prints &#8220;<i>Goodbye Cruel World<\/i>&#8221; because <code>#Hello<\/code> gets rid of the <i>Hello<\/i> part at the beginning of the string stored in <code>myvar<\/code>.<\/p>\n<p>You can use this feature alongside your globbing tools to make your <i>archive<\/i> duplicates:<\/p>\n<pre> for i in file0[12]?;\\ do\\ cp $i archive${i#file};\\ done <\/pre>\n<p>The first line tells the Bash interpreter that you want to loop through all the files that contain the string <i>file0<\/i> followed by the digits <i>1<\/i> or <i>2<\/i>, and then one other character, which can be anything. The second line <code>do<\/code> indicates that what follows is the instruction or list of instructions you want the interpreter to loop through.<\/p>\n<p>Line 3 is where the actually copying happens, and you use the contents of the loop variable <em><code>i<\/code><\/em><em> <\/em>twice: First, straight out, as the first parameter of the <code>cp<\/code> command, and then you add <i>archive<\/i> to its contents, while at the same time cutting of <i>file<\/i>. So, if <em><code>i<\/code><\/em> contains, say, <i>file019<\/i>&#8230;<\/p>\n<pre> \"archive\" + \"file019\" - \"file\" = \"archive019\" <\/pre>\n<p>the <code>cp<\/code> line is expanded to this:<\/p>\n<pre> cp file019 archive019 <\/pre>\n<p>Finally, notice how you can use the backslash <code>\\<\/code> to split a chain of commands over several lines for clarity.<\/p>\n<p>In part two, we\u2019ll look at more ways to use square brackets. Stay tuned.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After taking a look at how curly braces ({}) work on the command line, now it\u2019s time to tackle brackets ([]) and see how they are used in different contexts. Globbing The first and easiest use of square brackets is in globbing.&nbsp;You have probably used globbing before without knowing it. Think of all the times [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":90701,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-90700","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux-freebsd-unix"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/90700","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=90700"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/90700\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/90701"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=90700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=90700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=90700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}