Skip to content

Instantly share code, notes, and snippets.

@keinydev
Last active January 20, 2023 21:21
Show Gist options
  • Save keinydev/ff91bd7a51d0171ab71fc965db2d5a0c to your computer and use it in GitHub Desktop.
Save keinydev/ff91bd7a51d0171ab71fc965db2d5a0c to your computer and use it in GitHub Desktop.
Bash commands

Basic Bash commands


Bash Series

🔵 Basic Bash commands

Programming with bash

Bash Control Structure

User interaction with Bash


Clear the terminal screen

keinydev@laptop:~$ clear

Show current username

keinydev@laptop:~$ echo ~      #It returns complete path
keinydev@laptop:~$ whoami      #It returns username
keinydev@laptop:~$ echo $USER  #It returns username

List directories and files

keinydev@laptop:~$ ls

Navegate between directories

keinydev@laptop:~$ cd dir_name/

Show past directory if you have recently changed it

keinydev@laptop:~$ echo ~-

Create empty file

keinydev@laptop:~$ touch filename.txt

See the content of a file

keinydev@laptop:~$ cat file_name.txt

Break output into pages (to see less content)

keinydev@laptop:~$ cat file_name.txt | less #Navigate with arrows

See how many lines, words and characters there are in a file

keinydev@laptop:~$ cat file_name.txt | wc

Redirect values into a file

keinydev@laptop:~$ ls > dir_list.txt  #Saves the list of directories into a file. (It truncates the file content)
keinydev@laptop:~$ ls >> dir_list.txt #Saves the list of directories into a file. (It appends the content onto the end of the existing file)

Output and standar errors

keinydev@laptop:~$ ls /dir_not_exist 1>output.txt 2>error.txt #Creates two files with different contents. The output.txt file does not contain info because the directory does not exist and error.txt contains the error generated by the system

Print a Document

keinydev@laptop:~$ cat << EndOfText 
> Hello world
> This is a line.
> EndOfText
#Prints a document with break lines

Brace expansion (creates sets or ranges)

Prints from range

keinydev@laptop:~$ echo {1..10}   #Prints numbers from 1 to 10
keinydev@laptop:~$ echo {10..1}   #Prints numbers from 10 to 1
keinydev@laptop:~$ echo {01..100} #Prints numbers from 1 to 100 with padded zeros
keinydev@laptop:~$ echo {a..z}    #Prints letters from a to z
keinydev@laptop:~$ echo {z..a}    #Prints letters from z to a
keinydev@laptop:~$ echo {a..z..2} #Prints every two letters from a to z

Prints values with ranges

keinydev@laptop:~$ echo {banana,orange,lime}_{1..5} #Prints banana_1 banana_2 ... orange_1 orange_2 ... lime_1 lime_2 ...

Creates empty files with ranges

keinydev@laptop:~$ touch file_{01..10}.txt     #Creates file_01.txt, file_02.txt ...
keinydev@laptop:~$ touch file_{1..5}{a..d}.txt #Creates file_1a.txt, file_1b.txt..., file_2a.txt, file_2b.txt ...

Deletes all created files

keinydev@laptop:~$ rm file_*
keinydev@laptop:~$ rm file_{01..10}.txt #Deletes files with range and extension
keinydev@laptop:~$ rm file_{01..10}*    #Deletes all files with range if you dont know the extension

Show first line of each file

Suppose you have a same file name in different directories, you can see the first line of each one

keinydev@laptop:~$ head -n1 {dir1,dir2,dir2}/my_file.txt

Parameter Expansion

Interact with variables

keinydev@laptop:~$ car_brand="ferrari"    #Set a variable
keinydev@laptop:~$ echo $car_brand        #Prints the value
keinydev@laptop:~$ echo ${car_brand:3}    #Prints the last 3 chars of the value
keinydev@laptop:~$ echo ${car_brand:2:5}  #Prints the characters from position 2 to 5 (prints "rrari")
keinydev@laptop:~$ echo ${car_brand/r/s}  #Replaces the first "r" for an "s" instead (fesrari)
keinydev@laptop:~$ echo ${car_brand//r/s} #Replaces all the "r" for an "s" instead (fessari)

Command Substitution

Interpolation with commands

keinydev@laptop:~$ echo "My git version is $(git --version)" #Executes a command inside the string

Aritmetic expansion (Only calculations with integers)

keinydev@laptop:~$ echo $((2 + 2)) #Executes a the aritmetic operation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment