compare two values (comparing strings using <
or >
is a comparison of ASCII value)
String operators Number operators
= -eq
!= -ne
> > or -gt
< < or -lt
# use string operators when comparing alphanumerical values
[[ "$color" = "red" ]]
[[ "$color" != "red" ]]
[[ a = b ]] # false (comparing ASCII 97 = ASCII 98)
[[ a = a ]] # true (comparing ASCII 97 = ASCII 97)
[[ a > b ]] # false (comparing ASCII 97 > ASCII 98)
[[ a < b ]] # true (comparing ASCII 97 < ASCII 98)
# use number operators when comparing numerical values
[[ "$number" -eq 1 ]]
[[ "$number" -nq 1 ]]
display or set date and time
$ date
Tue Jul 15 09:36:41 EDT 2025
$ date -v1d -v+1m -v-1d -v-Friday "+%B %d"
July 25
write arguments to the standard output
$ echo Hello world!
Hello world!
$ echo -n Hello world!
iterate over a list and run commands for each list item
for anAnimal in dog cat zebra cow pig;
do
(( count++ ))
echo $anAnimal
sleep 1
done
for aNumber in {10..1};
do
echo $aNumber
(( aNumber-- ))
sleep 1
done
echo Blast off!
set or print name of current host system
$ hostname
Moosebook Pro II
return user identity
$ id
uid=501(talkingmoose) gid=20(staff) groups=20(staff),12(everyone),61(localaccounts),79(_appserverusr),80(admin),81(_appserveradm),98(_lpadmin),701(com.apple.sharepoint.group.1),703(com.apple.sharepoint.group.3),704(com.apple.sharepoint.group.4),702(com.apple.sharepoint.group.2),33(_appstore),100(_lpoperator),204(_developer),250(_analyticsusers),395(com.apple.access_ftp),398(com.apple.access_screensharing),399(com.apple.access_ssh),400(com.apple.access_remote_ae)
$ id -F
William Smith
$ id -u -n
talkingmoose
evaluate a test and execute additional commands based on whether the test succeeds or fails (also see test
)
if [[ a = b ]];
then
echo "carview.php?tsp=true"
else
echo "false"
fi
evaluate a test and execute additional commands based on whether the test succeeds or fails (also see test
)
$ echo $(( 2 + 2 ))
4
$ echo $(( 2 - 2 ))
0
$ echo $(( 9 * 8 * 7 + 65 + 432 - 1 ))
1000
$ thisNumber=1
$ thatNumber=4
$ echo $(( thisNumber + thatNumber ))
5
$ date "+%B %d"
July 15
$ date "+%s"
1752606000
$ start=$( date "+%s" )
$ echo $start
1752606000
$ stop=$( date "+%s" )
$ echo $stop
1752606300
$ echo $(( stop - start )) seconds
300 seconds
$ echo $(( number++ ))
0
$ echo $(( number++ ))
1
$ echo $(( number++ ))
2
$ echo $(( number++ ))
3
$ echo $(( number++ ))
4
$ echo $(( number++ ))
5
For a nerdy deep dive on whether you should use single or double square brackets, read Armin Briegel’s Single Brackets vs Double Brackets on his website Scripting OS X.
request response string
$ read myName
Bill
$ echo Hello $myName
Hello Bill
suspend execution for an interval of time (in seconds)
for anAnimal in dog cat zebra cow pig;
do
(( count++ ))
echo $anAnimal
sleep 1
done
print macOS system version information
$ sw_vers
ProductName: macOS
ProductVersion: 15.5
BuildVersion: 24F74
$ sw_vers -ProductVersion
15.5
reports system hardware and software configuration
$ system_profiler SPHardwareDataType
Hardware Overview:
Model Name: MacBook Pro
Model Identifier: MacBookPro18,3
Model Number: MKGP3LL/A
Chip: Apple M1 Pro
Total Number of Cores: 8 (6 performance and 2 efficiency)
Memory: 16 GB
System Firmware Version: 11881.121.1
OS Loader Version: 11881.121.1
Serial Number (system): JP24Z0XNRY
Hardware UUID: 9CC014E4-2746-5094-A77E-DB95E5E75548
Provisioning UDID: 00006000-000E29A43361801E
Activation Lock Status: Disabled
$ system_profiler SPHardwareDataType | awk -F": " /"Serial Number"/'{ print $2 }'
JP24Z0XNRY
condition evaluation utility (also see if-then-else
)
$ test a = a
$ echo $?
true
continue testing until a condition is met
color="green"
while [[ $color != "red" ]];
do
echo -n "What is your favorite color? "
read color
done
floor=1
clear
while [[ $floor -ne 5 ]];
do
echo "Current floor is $floor"
sleep 1
(( floor++ ))
done
echo "Ding! Floor 5: Athleisure wear"
The default macOS user shell since 2019.
A User's Guide to the Z-shell