Learning the bash shell, third look

Spread the love

i-30a1e0366512a8ac50ae2cf969f02d73-learning_bash_shell.jpgTime for another installment of our look at the book Learning the bash Shell (In a Nutshell (O’Reilly)). Today’s foray into Linux Land: Unix Command Substitution.There is more than one way to do this, but here is a sensible way to look at the format. In command substitution you stick something that comes out to be a valid UNIX command insice parentheses preceded with a dollar sign:$(UNIX command)The unix command is evaluated and then the result replaces the $() bit.For example, say you wanted to edit all of the files in the current directory that included the letters “html”. You could use the “grep” command to parse out all of the files to get a look at them. Grep goes through files and searches for a string, and by default spits out the line containing the string. There is an option (-l) that tells grep to spit out only the filename. So, for instance, I can do this:greg@greg-desktop:~/Desktop$ cd markdowngreg@greg-desktop:~/Desktop/markdown$ grep -l 'html' *4sh.htmlblog2.htmlblog2.txtblogrolJava.txtblogroll_master_SUboneyard.htmlboneyard.txtcalendar2htmldebate2.html...Then I could type these names into the text editor command.Or, I can turn this list of phrases into part of a command line. If I use “getdit” as the text editor, I can type”gedit grep -l 'html' *But that would not work! -l is not an available option for gedit, and there are other confusing things on this line to cause gedit to not get it. Get it? Good.BUt, if I put the mojo that spits out the list of files (which I tried above and know works) into the command sbustitution command, then then that will convert “grep -l ‘html’ *” into a a list of files.So this works:greg@greg-desktop:~/Desktop/markdown$ gedit $(grep -l html *) &This is the same as typing:greg@greg-desktop:~/Desktop/markdown$ gedit 4sh.html blog2.html blog2.txt …. and so on. The “and” sign at the end puts the operation into the background so I can keep using the same command line.

Have you read the breakthrough novel of the year? When you are done with that, try:

In Search of Sungudogo by Greg Laden, now in Kindle or Paperback
*Please note:
Links to books and other items on this page and elsewhere on Greg Ladens' blog may send you to Amazon, where I am a registered affiliate. As an Amazon Associate I earn from qualifying purchases, which helps to fund this site.

Spread the love

2 thoughts on “Learning the bash shell, third look

  1. Well, the goal is to edit the files that have html in them, not to edit a list of names of files that have html in them.Butgrep -l ‘html’ * | xargs gedit &will work for many people who don’t have bash as their shell.

Leave a Reply to rpenner Cancel reply

Your email address will not be published. Required fields are marked *