Normally, to do two commands you would use the && operator:

cd /usr && echo "I got to /usr"

When using &&, the second command will only run if the first one succeeds. For example:

der...@dereks-laptop:~$ cd /does-not-exist && echo "Huh?"
bash: cd: /does-not-exist: No such file or directory
der...@dereks-laptop:~$

   Note that "Huh" never got printed out, since the 'cd' failed.

Another way to do commands on the same line is to use the ; separator, as in:

cd /usr ; echo "I got to /usr"

That's equivalent to calling the two commands seperately, on two separate lines. So, the second command will get run even if the first one fails:

der...@dereks-laptop:/usr$ cd /does-not-exist ; echo "Huh?"
bash: cd: /does-not-exist: No such file or directory
Huh?
der...@dereks-laptop:/usr$

   Note that "Huh" printed out this time.

But the example that you gave -- running a command in an xterm -- is a special example, because the xterm runs a new shell process. You're not running xterm, and then running ps -ux. You're running ps-ux *within* the new xterm. So, the xterm program takes a special command-line option that says what command to "execute" in your new xterm. (It's the -e options, see 'man xterm'.) Other programs, like SSH and sudo, also take command-line options that tell them what program to run *within* the new child shell.

It's also possible to run two programs simultaneously, by putting one in the background with &, but I don't think that's what you're asking...

Shell scripting is something of a black art. Like Perl, there are many syntaxes to accomplish the same thing, and none of them are particularly intuitive.


--Derek

On 12/23/2009 08:31 PM, Thomas Taylor wrote:
I'm new to bash scripting and am wondering how to tie two or more commands together. As an example, to start an xterm session and run a command within it.
Start xterm & run "ps -ux in it.

I can do each separately in bash but haven't figured how to do them in unison.

Thanks for any help.

Also, what would be a good reference book for bash that's not too expensive?

Reply via email to