Skip to content

Instantly share code, notes, and snippets.

@saurabheights
Last active August 20, 2017 09:43
Show Gist options
  • Save saurabheights/04531abeca9b65609b29fa21c4f41a96 to your computer and use it in GitHub Desktop.
Save saurabheights/04531abeca9b65609b29fa21c4f41a96 to your computer and use it in GitHub Desktop.
# Thanks to http://ryanstutorials.net/bash-scripting-tutorial
# Original Author - Ryan Chadwick.
# If you are a complete newbie, the website will provide you much more knowledge, so please do check out his website.
# This is only a personal cheatsheet and there is a good chance I have skipped over some trivial stuff.
#Bash is case sensitive, so lower case variables are different the upper case variables.
#Path
echo $PATH
#Use : as separator.
PATH=$PATH:~/bin
#SheBang
#!/bin/bash
# Dont add any space in SheBang
#Variables
# In scripts, what variables are provided and how to access them.
# You can also use them on shell directly, not necessary for script only.
$0 - The name of the Bash script.
$1 - $9 - The first 9 arguments to the Bash script. (As mentioned above.)
$# - How many arguments were passed to the Bash script.
$@ - All the arguments supplied to the Bash script.
$? - The exit status of the most recently run process.
$$ - The process ID of the current script.
$USER - The username of the user running the script.
$HOSTNAME - The hostname of the machine the script is running on.
$SECONDS - The number of seconds since the script was started.
$RANDOM - Returns a different random number each time is it referred to.
$LINENO - Returns the current line number in the Bash script.
env - Listing of other variables
# Set a variable
variable=value # Don't use any space between variable and equal to sign as well as equal to sign and value.
# Take input
echo Please enter 13th largest prime number:-
read varname
echo $varname
# Single qoutes vs Double Qoutes
Single quotes will treat every character literally.
Double quotes will allow you to do substitution (that is include variables within the setting of the value).
myvar='Hello World'; echo $myvar
Hello World
newvar="More $myvar"; echo $newvar
More Hello World
newvar='More $myvar'; echo $newvar
More $myvar
# Command Substitution
myvar=$( ls /etc | wc -l )
# Command substitution is nice and simple if the output of the command is a single word or line. If the output goes over several lines then the newlines are simply removed and all the output ends up on a single line.
var=$(ls) # Run in a directory where ls gives multi-line output
echo $var
# For running script2 through script1:- Since, another script runs in a new process, thus to access a previously defined variables in script1 through this process, we must export it.
# What actually happens when we export a variable is that we are telling Bash that every time a new process is created (to run another script or such) then make a copy of the variable and hand it over to the new process.
# Call read with prompt
read -p 'Please enter 13th largest prime number: ' varname
echo $varname
# But to read password(silent mode) with prompt, do this:-
read -sp 'Please enter your password: ' varname
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment