LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-20-2012, 01:53 PM   #1
swamprat
Member
 
Registered: Sep 2005
Location: New Jersey, USA
Distribution: VMware V12 and V15 in Windows 10, MX Linux 23.1, Kubuntu 23.10, IBM z/VM 5.4
Posts: 567

Rep: Reputation: 34
Use of & in the bash shell.


Can anyone tell me what the '&' symbol does in the bash shell.

I do a 'echo &' and this shows me output as follows but being a newbie I have no idea what elese I can use the '&' sysbol for.

[root@centos5 ~]# echo &
[1] 3701
[root@centos5 ~]#

[1]+ Done echo
[root@centos5 ~]#
 
Old 07-20-2012, 02:02 PM   #2
Mr. Alex
Senior Member
 
Registered: May 2010
Distribution: No more Linux. Done with it.
Posts: 1,238

Rep: Reputation: Disabled
If you have your terminal started and you type in "medit" and hit Enter, medit will start and you won't be able to use that terminal until you close medit. But if you type in "medit &" you'll be able to use terminal without closing medit. So you can use one terminal instance to start any amount of apps you like with "&".
 
Old 07-20-2012, 02:04 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
A single ampersand at the end of a command forks the process into the background, so the shell can continue onto the next operation without having to wait for it to finish. The output you get is the process number, as reported by the shell's job control feature. It's rather pointless to use it with commands that terminate instantly, though, like echo.

The ABSG has a whole page that lists almost all characters that have special meanings.
http://www.tldp.org/LDP/abs/html/special-chars.html

And the Bash Guide covers most of the important basics like this.
http://mywiki.wooledge.org/BashGuide
 
1 members found this post helpful.
Old 07-21-2012, 03:55 AM   #4
Knightron
Senior Member
 
Registered: Jan 2011
Location: Australia
Distribution: openSUSE
Posts: 1,469
Blog Entries: 6

Rep: Reputation: 200Reputation: 200Reputation: 200
additionally, '&&' will run one command after another. example: the two commands, 'apt-get update', and 'apt-get dist-upgrade' are two separate commands but can be linked with the '&&' by:
Code:
apt-get update && apt-get dist-upgrade
 
Old 07-21-2012, 05:24 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608
BTW, I don't know if anybody noticed this, but testing things as root is one road to ruin, new Linux user or not. To minimize SNAFUs best use your unprivileged user account for that and only switch to root if required.
 
1 members found this post helpful.
Old 07-21-2012, 01:36 PM   #6
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Quote:
Originally Posted by Knightron View Post
additionally, '&&' will run one command after another. example: the two commands, 'apt-get update', and 'apt-get dist-upgrade' are two separate commands but can be linked with the '&&' by:
Code:
apt-get update && apt-get dist-upgrade
Actually, when && is used, the second command will only be run if the first command succeeds. This is because && is the logical AND operator. The way bash evaluates logical expressions, a logical AND will always fail if any of the subexpressions is FALSE. Therefore, if the first command in the string fails, the second will not be executed as the expression has already failed. The opposite behavior is given by || (logical OR), which will execute commands until any one of them succeeds. If you want all commands in a string to be run regardless of the success or failure of any individual command, they should be separated by semicolons ( ; ).
 
2 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash shell script: Str(007) to int(7),increment it(8) & convert back to string(008) drwatson_droid Programming 5 12-01-2011 10:44 AM
[SOLVED] Bash shell script to check the if empty files are being created & start the process hussa1n Linux - General 8 06-29-2010 05:49 PM
bash shell && and ; rosv Programming 3 01-25-2009 02:31 AM
[Req]: How to config WebSvr & DNS Svr from console by Bash shell script CiF Linux - Networking 2 03-22-2005 12:31 AM
bash shell scripting - && and || gui10 Programming 10 12-15-2001 03:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration