DEV Community

Cover image for πŸš€ The Missing Shell Scripting Crash Course

πŸš€ The Missing Shell Scripting Crash Course

Sahil Bondre on April 21, 2020

Bash is a shell and a command language. You can use it like any other language to write scripts. Bash scripts can run on Linux and Mac right off. F...
Collapse
 
crcastle profile image
Chris Castle

This is a great article! Once you get the basics down, ShellCheck is a great tool to use to make these and other syntax and style "rules" easier to remember (or not have to remember at all!). shellcheck.net

It can be easily added as a linter to Vim, Emacs, VS Code, and others.

ShellCheck screenshot

Collapse
 
godcrampy profile image
Sahil Bondre

This sounds like something I definitely need! Thanks!

Collapse
 
arnebab profile image
Arne Babenhauserheide • Edited

Thank you for your tutorial!

There’s one thing I see missing: If you use bash, you can do quick string operations in variables:

A=abc123foo.txt

strip suffix:
echo ${A%.txt} # abc123foo
strip suffix with globbing:
echo ${A%foo*} # abc123

strip prefix:
echo ${A#abc} # 123foo.txt
strip prefix with globbing:
echo ${A#*c} # 123foo.txt

Collapse
 
godcrampy profile image
Sahil Bondre

Thanks, that's really useful!

Collapse
 
dsbarnes profile image
dsbarnes

This is an excellent article. I've been working (less than I'd like to admit) on learning Bash. I've observed that floating point arithmetic is amazingly difficult (Or i've missed something obvious) and dealing with dates that are not GNU formatted typically yields my writing embarrassing and nonsensical pipes into sed / awk.

Any pointers for doing floating point math and handling dates for newbs like me?

Appreciate your sharing.

Collapse
 
godcrampy profile image
Sahil Bondre

Let me tell you a secret. Before writing this article I didn't know Bash myself. I've realized that teaching is the best way to learn. Maybe you can write a part two to this article explaining dates and floating-pointing arithmetic. I'm pretty sure you'll learn a lot from the process! Hope this helps πŸ˜ƒ

Collapse
 
pystar profile image
Pystar

Hello @dsbarnes ,
You can not do floating point arithmetic natively in Bash, but there is a trick I use i.e. using the 'bc' command like:

echo "3.142 + 3.142" | bc -l # add the value of pi to itself
echo "sqrt(49)" | bc -l # find the square root of 49
echo "scale=2; sqrt(91)" | bc -l # To find the square root of 91 to just 2 decimal places.

The capabilities of bc is extremely wide. Check the Manpage for its full documentation.

Collapse
 
bobbyiliev profile image
Bobby Iliev

This is a great article!

If you are a fan of opensource, feel free to contribute to the Introduction to Bash Scripting open-source eBook on GitHub!

Collapse
 
hoss3inf profile image
Hossein Farrokhi

on the begining of the tutorial, i had to use chmod +x ./script.sh instead of chmod -x ./script.sh to make it executable, am I missing something?

Collapse
 
godcrampy profile image
Sahil Bondre

Yup, yours is correct. I added the - out of habit! Fixed. Thanks πŸ˜„

Collapse
 
shlomilachmish profile image
shlomi-lachmish

Thanks for this fun handzone πŸ‘Œ

**small typo in section 6
[[ -n "$str" ]] # True if string is not of length zero

Collapse
 
godcrampy profile image
Sahil Bondre

fixed!

Collapse
 
thechazhall profile image
C.F. Hall

Thanks great article.

Collapse
 
godcrampy profile image
Sahil Bondre

I'm glad! And thank-you for reading!

Collapse
 
irregula_exprsn profile image
Muhammad Iliyas

πŸ‘πŸ½πŸ‘πŸ½πŸ‘πŸ½

Collapse
 
totallymustafa profile image
Syed Ahmad Mustafa

This is really useful, you saved me a lot of time. Also, great posts πŸ‘ŒπŸ»πŸ‘ŒπŸ».

Collapse
 
godcrampy profile image
Sahil Bondre

Thanks man!

Collapse
 
tslarge profile image
Travis Large • Edited

A well written and concise crash course! This is a great diving board into other shell scripting topics like simple automation.

Collapse
 
godcrampy profile image
Sahil Bondre

Thanks πŸ˜„