Learning the bash shell, third look

This is a repost of a review that is timely, given this week's focus on setting up your Linux server and changing all your computers over to Linux and so on.

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 markdown
greg@greg-desktop:~/Desktop/markdown$ grep -l '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.


More like this

A repost, continuing along the lines of bashing the shell. Having examined Learning the bash Shell (In a Nutshell (O'Reilly)) (see here, here, and here), it is now time to turn to a more advanced reference to help you geek out on your Linux computer. If you want to have only one book on bash,…
This is a repost of a review that is timely, given this week's focus on setting up your Linux server and changing all your computers over to Linux and so on. I started this discussion a while back, and now it is time to continue it. The Bourne Again Shell, bash, is the default command-line shell…
I wrote earlier of the very useful command line utility called gcalcli (short for GoogleCALendarCommandLineInterface). Click here to read that post. One of the options is called "agenda" which spits out, by default, the next five days of calendar entries. If you would prefer a different range of…
I just got a copy of The Linux Command Line: A Complete Introduction. I read one review of it a while back which was quite positive, suggesting that the book was both really useful and really not boring. Here's the description from the publisher: You've experienced the shiny, point-and-click…

I don't know if it's a stylistic thing, but I've always used backticks (`) to enclose command results to be passed to other commands. This is the first I've heard of $(command), and I'm playing with both now to see if there are any quantitative differences.

They are the same ... $() is the posix form and `` is only for backwards compatability, or so it says in the GNU bash manual and some other references.

But can it return real objects like the Windows power shell can? Instead of just text?

I'm only kidding, by the way...

Allegedly they're working on a power shell for linux too. I'd like to use linux all the time and the first thing I do is download cygwin. A lot of people are probably in the same boat too.

Researchers at Duke University have concluded that monkeys don't respond solely to direct punishment or rewards for their actions. They can, according to the scientists, actually adjust future behavior when shown the reward or punishment they would have received had they chosen to behave differently.