Sunday, January 16, 2022

bash tutorial - part 1

 boom bang bash ! tutorial on-click.

 all code in this tut (not only post) will be indicated by a underline. most important parts should be bold or caps-locked.

okey so 3 importances as for first time :

1) everytime you write shell files they should  start with #!/usr/bin/env bash 

why? most tutorials will say you that it should be #!/bin/bash or #!/bin/sh . im saying it should be env . just because of compatibility. user can have bash installed in other directory than /bin . path /usr/bin/env lists all installed shells in all the system and searches for bash using second argument. it doesnt look for exact path.

2) every shell file should end with .sh . BUT there is exception - you can remove extension and put script to your binaries list e.g. /bin or /usr/bin . it'll work there like a command without providing path to file (how do you use su, mkdir or wget commands?)

3) every shell script need to be marked as executable (jeez i need to say it? ). to do it you can use one of two commands (or use your file manager to do so) : chmod +x {file} OR chmod 777 {file}

 

so here we start. okey. some info to note:

• $ indicates variable

• every code block should be separated by ;

   (if you are gonna call 2 commands)

• if any variable contains more than 1 word you need to put it in quotes "like this"

!!! warning!!! bash differents '' single quotes from "" double quotes. single quotes cant use variables and meta chars (like new line) where double quotes can.

and okay. we already have many things. now some meta chars about which i was already talking :

\a   -   ring (generally sound, notification)

\n   -   new line

\\   -   backslash !!! dont use single backslash cause it will be used as nothing. !!!

\t   -   tab (this you know mostly - horizontal)

\v    -   vertical tab

\b   -   backspace (yes there is a shortcut for this, so you can remove part of output on condition <- this will be later)

\'   -   single quote (displayed)

\"   -   double quote (displayed)

last part of post - variables (we will make first executable script) :

example of variable:

FILE=bash.sh

where first part of variable is name, secondly is = char and then are contents of variable (eg. file name, text in quotes or command to execute later). if you will use it - add before $ sign for bash to indicate variable instead of text

example of variable usage :

$FILE

and one important info about which i almost forgot : making comments. bash supports only one-line comments (you cant make a block comment from few lines). and you make a comment just by typing # . no im not joking, similar way to indicating bash source. there is only one thing - when you indicate executor you need to immediately after # put ! . when you make a comment you need to leave one space after # . and showing in terminal output of script (custom). just write echo, make space and put in " " all text you want to output.

okey last thingy - script which after reading this post you will surely understand :

#!/usr/bin/env bash

# setting variable:

 VARIABLE="outputted text"

# outputting variable:

echo "$VARIABLE\nwas written int\b \n \t bash \a"

# also notice all slashes in text. there are 2 line breaks, horizontal tab, alert and hidden backspace to fix typo + added variable.

No comments:

Post a Comment