Skip to content

Instantly share code, notes, and snippets.

@shreewatsa
Last active February 4, 2024 07:29
Show Gist options
  • Save shreewatsa/a29107cc5a9a7794aba1977d48b49f0c to your computer and use it in GitHub Desktop.
Save shreewatsa/a29107cc5a9a7794aba1977d48b49f0c to your computer and use it in GitHub Desktop.
Shell Scripting Tips and Tricks

Get Linux OS name and version (4 ways):

$ lsb_release -a;  
$ cat /etc/os-release;
$ cat /etc/issue;
$ hostnamectl;

Run a command that is shadowed by an alias:

$ls;              #Prefix command with backslash  
$command ls;      #Use ‘command’ utility  
$/bin/ls;         #Use absolute path  
$“ls”;            #Quote the command  
$unalias ls ;     #Temporarily unalias that command  

List all aliases and know what an alias does:

$type pwd;      # Use ‘type’ to find out what the program does. Helpful to find out the alias commands.  
$alias;         # Display all the custom aliases.  
$compgen -c | grep -x . | sort -u;  # List all the single character linux commands.  
$type -m ‘?;   # Similar to above ‘compgen’ command but for zsh.  
$type -a ls;    # To troubleshoot what is happening with the ls command.  

Run processes in background:

nohup : no hang up. nice: the priority of the process is called nice value. default is 10.

$nohup htop &;        # (1) Using nohup.
$nohup nice htop &;   # nice: run program in lower priority.
$((htop &)&);         # (2) Using double fork.
$Ctrl+z               # moving the current program in background.
$fg;                  # Bring background job to foreground. ie reverse of ^z .

$sleep 10 &;         # sleeps for 10 seconds.
$jobs;               # List all the background running jobs.

$sudo nohup htop > nohup_log.log &;       # Redirecting the nohup output to nohup_log.log file rather than the default nohup.log file.
$nohup bash -c 'mkdir files &&ping -c 1 google.com && ls'> output.txt;     # Running multiple process in background using nohup.
$kill -9 $(pgrep –a htop);                # Killing a process using PID.

Usages:

zathura book.pdf & ; disown;  #works
zathura book.pdf >/dev/null 2>&1 & disown;  #works

Install nerd fonts:

$brew install $(brew search font | grep -i nerd | tr '\n' ' ');

Clear terminal window (5 ways): clear, reset, ^L, printf "\ec", printf "\x1Bc"

ps:

$ps -o pid,pgid,tty,etime,comm | grep zsh;      # pgid: process group id.
$echo $(ps -ef | grep gunicorn | awk '{print $2}' | sort | uniq | egrep -v '^1$') | xargs kill;
$ps -9 <pid>;                                   # Get program name from it’s pid.
# <defunct>    # zombie processes, already killed.

Sourcing files in bash/zsh:

if [ -f ~/.config/commons/bash_aliases ]; then  
    . ~/.config/commons/bash_aliases  
fi
# [[ ! -f ~/.config/commons/bash_aliases ]] || source ~/.config/commons/bash_aliases    # Alternative

Shell tricks:

$echo $_ ; echo !$ ;    # Last argument of previous command.
$echo !! ;              # Last command.

Updating Password Policies (Example):

$ vim /etc/pam.d/common-password 
# Note: comment out all the lines in this file and add the following lines at the EOF.
password [success=1 default=ignore] pam_unix.so minlen=1 sha512 
password requisite pam_deny.so 
password required pam_permit.so 
password optional pam_gnome_keyring.so
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment