Git Product home page Git Product logo

shell-scripting-cheat-sheet's Introduction

shell-scripting-cheat-sheet

Small, sweet shell things.

Motivation

I don't know anything about shell scripts. Have always been scared and confused. Currently I am having to process a ton of audio files and had to not repeat myself doing same things over and over again. So here I am, learning shell things!

Variables

  • While assigning a value to a variable, don’t use space around =.
# Good
APPLE='is a fruit'
# Bad
APPLE = 'is a fruit'
  • Print variables within quotes to preserve their whitespace if they contain any.
$greeting='Hello      World'
echo $greeting # Hello World
echo "$greeting" # Hello      World
  • To not have $ treated as a special character denoting a variable inside echo, use backslash \
echo "\$greeting" # $greeting

Useful for printing things like 'Amount : $200.00'

  • Within quotes, variables can also be printed like ${MY_VARIABLE}. This is to reduce ambiguity.

  • The output of commands can be assigned to variables directly. Use back ticks or $() to wrap around the command.

FILES_LIST=`ls`
FILES_LIST=$(ls)

Script/Command Arguments

  • Getting arguments passed to a script is easy. $1 is the first argument. $2 is the second and so on.
    • TIP: assign the arguments to properly named variables inside scripts. Example - DIRECTORY=$1

Arrays

# Define an array
MY_ARRAY=('apple' 'banana' 'orange')

# All items of the array
echo ${MY_ARRAY[@]} # apple banana orange

# Length of an array
echo ${#MY_ARRAY[@]} # 3

# Get value by index
echo ${MY_ARRAY[2]} # banana

Strings

# Define
MY_STRING="Hi! I am a String"

# Print entire string
echo $MY_STRING
echo $MY_STRING[@]

# Length
echo $#MY_STRING # 17

# Get substring
# Syntax: ${STRING:START_POSITION:LENGTH}
echo ${MY_STRING:0:3} # Hi!
# If LENGTH is removed, substring till end of string.
echo ${MY_STRING:4} # I am a String

# Replacing
# First occurence
echo ${MY_STRING[@]/a/o} # Hi! I om a String
# All occurences
echo ${MY_STRING[@]//a/o} # Hi! I om o String
# with another variable or expression
TEMP='an Idiot'
echo ${MY_STRING/a String/${TEMP}} # Hi! I am an Idiot
echo ${MY_STRING/String/String on $(date)} # Hi! I am a String at Wed Jan  9 17:03:09 IST 2019

shell-scripting-cheat-sheet's People

Contributors

praveenpuglia avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.