The Random Byte
  • Home
  • Cloud
  • Investment
  • Portfolio
No Result
View All Result
  • Home
  • Cloud
  • Investment
  • Portfolio
No Result
View All Result
The Random Byte
No Result
View All Result
Home Foundations

Bash Random bytes and Tricks

Nik by Nik
September 30, 2022
in Foundations
165 9
0
terminal shell
538
SHARES
1.7k
VIEWS
Share on FacebookShare on Twitter

I have been struggling with the linux bash shell and wanted to learn by doing some hands-on. So here it is…

Lets get the basics right in a minute:

1. Intro: When you execute any command on the bash shell like “ls”, its actually invoking:

  • either an external executable file (placed in the disk at /bin/ls or /usr/bin/pwd or /usr/bin/cat)
  • or a built-in command/function as part of bash shell itself (like cd, history)
  • or its a reserved keywords (like for, case, while)

To verify this, use the “type -a <commandname> ” on the bash shell and you can read through the output.


2. Exit on failure: Every command produces an exit code. Heres the plain way to check it:

When is it important? If you are writing a shell script with multiple commands and want to halt the script if any of the command fails.

So use below command at the start of the script to exit if any command fails with a non-zero exit code

# exit when any command fails
set -e

3. File Commands: To extract a filename from a path or the last directory name, use the “basename” command.

If you want to strip out the file extension also, then do this:

If one needs the directory structure, shell has you covered with the dirname command:

When you can use the dirname in real life? In a shell script, and want to switch folders (dynamically from wherever the script is run from), use like this:

$(dirname $0)/../<newfolderpath>

4. Import: Import another script into your script:

source $(dirname $0)/<newscriptpath>              # if the script is in same folder
source $(dirname $0)/../<folderpath>/<scriptpath> # if the script is in diff folder

5. Return Status: return value from each executed command can be captured by a special variable $?


6. MD5: Sending a file/directory across the network is bound to have risks, which can be prevented by a linux utility like md5sum

run the md5sum * > digest which creates a hash value for all the files and the clients can run md5sum -c digest at their end to verify all the hashes match with the digest file (OK status).

If there was any file modified during the transfer, the md5sum command will not show the OK status.


Previous Post

Golang, from Dev to Prod

Next Post

REST Web Services, What is it

Nik

Nik

Engineer | Developer | Cloud and Automations | Lifelong builder

Next Post
REST Web Services, What is it

REST Web Services, What is it

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Overloads/Congestion Control
  • AWS Multi-Region Architecture
  • A Block Storage called S3
  • DNS with Route 53 (for Hybrid Cloud)
  • Event Driven Architecture

Recent Comments

No comments to show.
April 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
27282930  
« Jan    

Archives

  • January 2024
  • June 2023
  • May 2022
  • February 2022

Categories

  • Automation
  • Cloud
  • Foundations
  • Programming
  • Contact
  • Investment
  • Landing
  • New Home
  • Portfolio

© 2026 JNews - Premium WordPress news & magazine theme by Jegtheme.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
No Result
View All Result
  • Contact
  • Investment
  • Landing
  • New Home
  • Portfolio

© 2026 JNews - Premium WordPress news & magazine theme by Jegtheme.