Wednesday, July 27, 2011

Subshells


Sometimes, you need to execute several commands together. For example, if you want to perform a specific action in a different directory, you could use the code in Listing 8.

Listing 8. Execute several commands at the same time

# pwd
/home/cormany

# cd testdir

# tar รข€“cf ls_output.tar ls.out?

# pwd
/home/cormany/testdir


This works, but note that after the execution of theses steps, you're no longer in your original directory. By placing the commands into their own subshell, they execute as a single instance of the subshell. Listing 9 shows the same idea executed using a subshell.

Listing 9. Execute several commands at the same time using a subshell

# pwd
/home/cormany

# (cd testdir ; tar -cf ls_output.tar ls.out?)

# pwd
/home/cormany 

No comments:

Post a Comment