![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Starting to use Unix
I'm just starting unix and I've learned most of the basics like browsing through directory trees, creating files using 'vi' and such (well way more than that actually.. lol).
So today I decided to move onto scripts. One thing that I don't understand is why use the 'bash' command or (bourne shell) before creating a script. I understand that going into the bourne shell is where you should be scripting. What is the different shell for? Why not just stay in whatever shell you're using. Is there any advantage to using the bourne again shell (the bash command) over the default shell?
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Are you talking about the shebang at the top of the file?
#!/bin/bash |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Yes, I've been told to write the 'bash' command then create my script in that shell.
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 2
![]() |
Well what you've been told is very confusing. If you're familiar with windows the bash is sorta like the command prompt and a script is like a bat file.
Though is practise that's a gross over simplification.
__________________
Visit my website BinaryNotions. |
|
|
|
|
|
#5 |
|
Programmer
|
you can create a script from any editor, so you could start an emacs from a csh or run an instance of SciTE on a remote computer and still write a bash script. 'bash' is the most commonly used shell, but 'sh' is the only one you can guarantee to exist - bash is fully compatible with any vanilla bourne sh.
The first line of a shell script, or any script (including python, perl, ruby...) tells the system how to run it if it begins with '#!' this means that #!/bin/sh |
|
|
|
|
|
#6 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Quote:
For instance, I could be running dash, and create a shell script that will run using bash: dash$ cat > shellscript.sh #!/bin/bash echo Running using $SHELL dash$ chmod +x shellscript.sh dash$ ./shellscript.sh Running using /bin/bash |
|
|
|
|
|
|
#7 |
|
Expert Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 706
Rep Power: 3
![]() |
To expound on what free-zombie said a bit, I actually just asked about the #! syntax in another thread recently. There were some good replies, and I recommend that you read it if you haven't already.
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
#8 |
|
Unverified User
Join Date: Aug 2005
Location: none
Posts: 146
Rep Power: 0
![]() |
Kinda off-topic, but you should use sh instead of bash at the beginning of the script, unless the script uses some feature in Bash that standard sh doesn't have for compatiblity with systems that don't have Bash (FreeBSD doesn't come with Bash by default).
__________________
Warning: My posts may change (dramatically) within the first 15 minutes they're posted. Got 'Nux?—GNU/Linux and other free software support. It's GNU/Linux, not just Linux. |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Jan 2008
Posts: 2
Rep Power: 0
![]() |
Re: Starting to use Unix
why some of the scrip file is without the starting #!/bin/bash or any shell, and can straight away coding? what is the different between with starting #!/bin/bash or without the #!/bin/xxx in the starting script?
sorry i'm new in unix.. Thanks |
|
|
|
|
|
#10 | |
|
Hmmmm ... Is there more??
Join Date: Apr 2008
Location: Post Falls, ID
Posts: 15
Rep Power: 0
![]() |
Re: Starting to use Unix
Quote:
If you are writing a script that you know, with 100% certainty, will always be run on a machine where the only available shell is the one you're using to write the script - then this line is completely optional. If, however, there exists a possibility that some user will use a different shell - then you MUST specify the shell to be used for your script. For example - if you use a script command that only exists in the /bin/csh shell - and your user happens to be using the /bin/sh shell - it is very likely that your script will fail. (I almost would guarantee failure in this instance...) But, if you force your script to run in a specific shell using the !shebang then your script will function correctly.
__________________
Ken - New to PFO ... but been dabbling in various versions of BASIC since highschool - circa 1973. "Shouldn't the 'Air and Space' museum be empty?" - Dennis Miller |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Starting a career... start-ups worth it? | Jimbo | Coder's Corner Lounge | 12 | Nov 2nd, 2006 12:23 PM |
| Unix commands compatible with Windows? | titaniumdecoy | Bash / Shell Scripting | 7 | Oct 5th, 2006 7:25 AM |
| Lions' Commentary on UNIX 6th Edition, with source code | Mad_guy | Book Reviews | 0 | Sep 24th, 2006 7:06 PM |
| Best Linux or Unix Desktop? | Prm753 | Coder's Corner Lounge | 23 | Apr 8th, 2006 10:17 AM |
| Importing from DLL's on Unix | Kaja Fumei | Existing Project Development | 5 | Jan 9th, 2006 6:35 PM |