b5media.com

Advertise with us

Enjoying this blog? Check out the rest of the Technology Channel Subscribe to this Feed

New Linux User

Explain: Command Substitution

by Jon on April 10th, 2006

I don’t know if this should be a HOWTO or an Explanation, but I’ve decided on Explanation.

The idea behind command substitution is to construct a command that contains a dynamic portion. What the hell does that mean? Well, think of an environment variable like the $HOME variable. On pretty much all standard setup Linux boxes, the $HOME variable is exported into the environment when a user logs on. The $HOME variable contains, as you can probably expect, the value of the current user’s home directory. Try it - fire up a terminal window and type

echo $HOME

On my system, I get: /home/jon

This is handy little thing to know if I’m writing a script that, say, copies a file to a user’s home directory. Without knowing the account name of every possible user that might run this script, I can simply use the $HOME variable as the destination for my file copy.

Command substitution is a little more complex in that I can use a command rather than a variable. Think of it like calling a function for you programmers amongst us.

I first ran across this when grabbing kernel headers because you want to ensure that you get the right headers. There are two ways to do this: using the backtick ` character or the $() characters.

Try this:

echo linux-headers-`uname -r`

On my system that nets me: linux-headers-2.6.12-9-386. Note that the backtick isn’t the single-quote. On a US-layout keyboard, the backtick is usually on the same key as the tilde ~ key. But not always :)

I can get the same result by using:

echo linux-headers-$(uname -r)

The possibilities are pretty much endless.

Enjoy!

POSTED IN: Explanation

0 opinions for Explain: Command Substitution

  • No one has left a comment yet. You know what this means, right? You could be first!

Have an opinion? Leave a comment:




Check Spelling
Activate Spell Check while Typing