Git Product home page Git Product logo

learnyoubash_notes's Introduction

IMPORTANT NOTES ABOUT BASH

File set-up

First line of the file should contain #!/usr/bin/env bash. This tells the system what program to use (in this case, Bash).

To make a file executable, change its read/write values..

chmod +x file.sh

Variables

Only two data types (or more appropriately, zero data types) -- Strings and Numbers

  • To declare a variables, use = with no spaces.
  • To display a variable, use the echo command.
  • To delete a variable, use the unset command.
foo="Andy"
echo $foo       # > Andy
unset foo       # deleted

Single Quotes vs. Double Quotes

  • Variables contained within double quotes will be expanded / evaluated.
  • This is not the case with single quoted strings.
NAME="Denys"
echo "My name is $NAME"   # > My name is Denys
echo 'My name is $NAME'   # > My name is $NAME

Environmental Variable

To expose a variable beyond the file scope, declare its value with export.

export MY_GLOBAL="see/me/from/anywhere"

note: There are a lot of global variables in bash. You will meet these variables fairly often, so here is a quick lookup table with the most practical ones:

Variable Description
$HOME The current user's home directory.
$USER The current user.
$PATH A colon-separated list of directories in which the shell looks for commands.
$PWD The current working directory.
$RANDOM Random integer between 0 and 32767.
$UID The numeric, real user ID of the current user.
$PS1 The primary prompt string.
$PS2 The secondary prompt string.

Positional Parameters

Allocated when the function is evaluated. Here's a list:

Parameter Description
$0 Script's Name
$1 … $9 The parameter list elements from 1 to 9.
${10} … ${N} The parameter list elements from 10 to N.
$* or $@ All positional parameters except $0.
$# The number of parameters, not counting $0.
$FUNCNAME The function name (has a value only inside a function).

In this example, ./script.sh foo bar

# the parameter values would be...

# $0='./script.sh'
# $1='foo'
# $2='bar'

Default parameter values

Variables/parameters can have default values defined with {VAL:-'defaultValue'} syntax.

# Assign FOO value 'default' if it's empty
FOO=${FOO:-'default'}

learnyoubash_notes's People

Contributors

newswim avatar

Watchers

 avatar  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.