Bash shell scripting in OS X

Joined
Oct 30, 2010
Messages
273
Reaction score
0
Points
16
Location
Southern Illinois
Your Mac's Specs
MBP 15" I7 first one and loving it.
I'm wanting to learn bash scripting for OS X. Could someone recommend a good book or resources I could use?

I'm also interested in command line in terminal. I know Unix OS X commands are somewhat different than Linux or Unix. Also would like any resources for learning these commands.

Thanks
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
The Linux Documentation Project's Beginner Guide and Advanced Guide would be a good start. Although the beginner one is nearly 3.5 years old, it should still do the job (and worst case you Google the difference/problem).
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
The Linux Documentation Project's Beginner Guide and Advanced Guide would be a good start. Although the beginner one is nearly 3.5 years old, it should still do the job (and worst case you Google the difference/problem).

yea, not much has really changed significantly in those 3.5 years.
 
OP
V
Joined
Oct 30, 2010
Messages
273
Reaction score
0
Points
16
Location
Southern Illinois
Your Mac's Specs
MBP 15" I7 first one and loving it.
Thanks looks like a good starting point.
 
Joined
Jan 12, 2009
Messages
1,096
Reaction score
19
Points
38
Location
Prague, Czech Republic
Your Mac's Specs
2,4Ghz 15" unibody
Mac OS has (from what I can tell) a standard bash shell, it's rare that I run into differences. So any bash scripting book as suggested above is great. Basically it's bash with some additional OSX delivered programs - e.g. changing defaults or ... ooooh, "say" :-D

If you want to have a look at something specific to Mac OS, have a look at AppleScript, which can be amusing :-D

Well and do what the rest of us do: need to accomplish a task? google the script someone else has already used and posted in a forum, on their blog and such ;-)
 
Joined
Feb 26, 2010
Messages
2,116
Reaction score
123
Points
63
Location
Rocky Mountain High, Colorado
Your Mac's Specs
1.8 GHz i7 MBA 11" OSX 10.8.2
Shell Scripting Primer
https://developer.apple.com/library...tml#//apple_ref/doc/uid/TP40004268-TP40003516

How to use Manpages
https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/

The Apple Manpages
https://developer.apple.com/library...anpages.5.html#//apple_ref/doc/man/5/manpages

Install Command line tools from xcode for development
Xcode -> Xcode -> Preferences -> Downloads -> Command Line Tools -> Install

Shell Here Opens a shell in the current Finder Folder
Download ShellHere 1.0.5 Free - Open a new Terminal window from Finder - Softpedia

My setup for .profile
Code:
alias ls='ls -GF'
alias ll='ls -hGlF'
export PS1='\e[0;31m\w\e[m\n\T (\! \j) > '
#export CLICOLOR='1'
#ansi colors for ls
# a     black
# b     red
# c     green
# d     brown
# e     blue
# f     magenta
# g     cyan
# h     light grey
# A     bold black, usually shows up as dark grey
# B     bold red
# C     bold green
# D     bold brown, usually shows up as yellow
# E     bold blue
# F     bold magenta
# G     bold cyan
# H     bold light grey; looks like bright white
# x     default foreground or background
#order of LSCOLORS
# 1.   directory
# 2.   symbolic link
# 3.   socket
# 4.   pipe
# 5.   executable
# 6.   block special
# 7.   character special
# 8.   executable with setuid bit set
# 9.   executable with setgid bit set
# 10.  directory writable to others, with sticky bit
# 11.  directory writable to others, without sticky bit
#                1 2 3 4 5 6 7 8 9 1011
export LSCOLORS='Hxfxcxdxbxegedabagacad'

Books
Probably the best condensed reference for everything unix
http://www.amazon.com/Nutshell-Fourth-Edition-Arnold-Robbins/dp/0596100299?tag=macforums0e4-20

Bash Shell Book
Amazon.com: Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly)) (9780596009656): Cameron Newham: Books

The best regular expressions book
http://www.amazon.com/Mastering-Reg...words=regular+expressions?tag=macforums0e4-20
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
Note that although IvanLasston's .profile is quite elaborate and might prove useful at some point, it's not necessary for the purposes of scripting.
 
Joined
Feb 26, 2010
Messages
2,116
Reaction score
123
Points
63
Location
Rocky Mountain High, Colorado
Your Mac's Specs
1.8 GHz i7 MBA 11" OSX 10.8.2
The OP mentioned
I'm also interested in command line in terminal.
So I posted my .profile - as the dircolor command is quite different from Linux/Unix to Mac.
 
OP
V
Joined
Oct 30, 2010
Messages
273
Reaction score
0
Points
16
Location
Southern Illinois
Your Mac's Specs
MBP 15" I7 first one and loving it.
I'm finding it difficult to learn from the included links. As below

set VALUE ="Four"

echo $VALUE
# or...
setenv VALUE "Four"

echo "$VALUE score and seven years ago...."

This is from Shell Scripting Primer listed above. It is wrong as best as I can tell. For the bash shell it should be.

VALUE="Four"
echo $VALUE

I just keep running into tutorials that are not right. I spend most of my time trying to figure out what is wrong. Not sure if it is just the different commands in OS X command line or what. Anyone offer any guidance to me on this issue?
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
So I posted my .profile - as the dircolor command is quite different from Linux/Unix to Mac.
Would that not be shell dependent and not OS dependent?

I just keep running into tutorials that are not right. I spend most of my time trying to figure out what is wrong. Not sure if it is just the different commands in OS X command line or what. Anyone offer any guidance to me on this issue?
You need to make sure you read Bash tutorials. There are multiple "shells" available to Unix and Unix-like operating systems and they each have their own idiosyncrasies. I've also noticed that it is very unforgiving with syntax errors.
 
Joined
Feb 26, 2010
Messages
2,116
Reaction score
123
Points
63
Location
Rocky Mountain High, Colorado
Your Mac's Specs
1.8 GHz i7 MBA 11" OSX 10.8.2
Would that not be shell dependent and not OS dependent?

The profile itself is bash - so yes if you are using a csh or variant you need to adjust accordingly. The dircolor is the part that is really different from other OS variants.

@V6Pony
The example you are giving is csh. Generally - you'll have to pick a shell and stick with it when you are first learning. From the command line

Code:
echo $SHELL

It should be bash - so anything csh will not work. You'll need to follow the bash examples.
 
OP
V
Joined
Oct 30, 2010
Messages
273
Reaction score
0
Points
16
Location
Southern Illinois
Your Mac's Specs
MBP 15" I7 first one and loving it.
ivanlasston I see. I thought that was a bash tutorial. It seems also that Darwin Unix bash "Apple" is different than linux bash and so on. It's really hard to find a tutorial or book where the commands are correct. Where the commands work in apple terminal.
 
Joined
Jan 12, 2009
Messages
1,096
Reaction score
19
Points
38
Location
Prague, Czech Republic
Your Mac's Specs
2,4Ghz 15" unibody
ivanlasston I see. I thought that was a bash tutorial. It seems also that Darwin Unix bash "Apple" is different than linux bash and so on. It's really hard to find a tutorial or book where the commands are correct. Where the commands work in apple terminal.

From what I can tell the OSX-delivered bash is the same as on Linux.

If you follow bash tutorials that were meant for various linux distributions, it will work as far as scripting or file management is concerned :) But it can get a bit confusing from the beginning to get your head wrapped around the different shells, as recommended above, stick with bash - it's something like a standard or a default shell on most systems.

Just skip any tutorials and examples concerned with configuring the system for now (I mean stuff like setting the network, displays, devices ...)
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
The dircolor is the part that is really different from other OS variants.
Ah, didn't know that! Thanks.

ivanlasston I see. I thought that was a bash tutorial. It seems also that Darwin Unix bash "Apple" is different than linux bash and so on. It's really hard to find a tutorial or book where the commands are correct. Where the commands work in apple terminal.
Shell scripting is fairly uniform across platforms. The major differences comes in to the tools you use during the actual scripting (aside from, as IvanLasston noted above, some minor differences). Care to post an example of something that is causing you some issues?

This is why I like scripting languages - everything works like a charm regardless of the shell or OS.
 
Joined
Feb 26, 2010
Messages
2,116
Reaction score
123
Points
63
Location
Rocky Mountain High, Colorado
Your Mac's Specs
1.8 GHz i7 MBA 11" OSX 10.8.2
The script is usually the same - where I've run into problems is in the options available between the different tools - as you said vansmith. What I tend to do is google both the linux and osx commands. So I google
Code:
man ls mac
man ls
man find
man find mac
All in different windows of course. The man/mac google should drop you into the developer man pages that I linked to before. The straight man - should find you a standard linux man page. You'll see that some of the built in commands do not match up in arguments. So if you do something like ls with options and dump that into a file to manipulate - you may get an error saying that option doesn't exist. (this is also why dircolor may bite you) Just an example

find command
https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/find.1.html
UNIX man pages : find () (outdated link removed)

ls command
Loading…
UNIX man pages : ls () (outdated link removed)
 
Joined
Jan 21, 2016
Messages
1
Reaction score
0
Points
1
Difference between MAC and Linux scripting

I have script like this
csh bbox.csh plt >! plt_P1.ps
Guy who wrote it used Darwin I am on Ubuntu 14.04.How to change it?
What does >! mean?
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
csh bbox.csh plt >! plt_P1.ps


So that executes box.csh with an option (at least that's what it looks like), then redirects standard output ( >! is csh for redirect stdout) to the file plt_P1.ps... and it WILL overwrite the file if it already exists.


btw, this is csh shell, not bash. You can do either, or both, in both linux and os x.. well and aix, and.. and... and.. :)
 
Last edited:

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
I have script like this
csh bbox.csh plt >! plt_P1.ps
Guy who wrote it used Darwin I am on Ubuntu 14.04.How to change it?
What does >! mean?
Change it how? Are you looking to modify that command or one of the scripts?
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
Change it how? Are you looking to modify that command or one of the scripts?

I don't see a need to change the script, it's csh...

Code:
mike@beer ~ $ which csh
/bin/csh
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top