Geody Labs


# Main Index: Debian Linux Magic Spells Cheat Sheet (one liners, how to, tips and tricks)

# Bash Shell

Show full name of the terminal type:
tput longname

Show BASH version:
echo $BASH_VERSION

Command Line Navigation:
CURSOR LEFT / CURSOR RIGHT # move cursor one character left or right on command line
LEFT ALT+[B/F] # move cursor to previous / next word on command line
CURSOR UP / CURSOR DOWN # move to previous / next command in history
CTRL+R # search for a command in History: hit CTRL+R, type the string you want to search, hit CTRL+R again to find further matches

Switch bash shells:
LEFT ALT+[F1-F6]

Cycle between bash shells:
LEFT ALT+[CURSOR LEFT / CURSOR RIGHT]

Show current terminal:
tty

Terminal line settings:
stty -a # Print all terminal line settings in human readable format
stty -g # Print all terminal line settings in stty readable format

Reset a "corrupted" shell:
reset # It will also reset the terminal terminal window size to 80 x 36 characters

Clear shell terminal window:
clear # Clear screen (if your terminal has a buffer, you can still scroll back to see the text previously on screen)
printf "\033c" # clear the screen by scrolling all the text above the visible area (same as CTRL+L)

Set Terminal Window Size:
echo -e "\e[8;LINES;COLUMNSt" # resize the terminal window to LINES x COLUMNS . For example, tlins=40; tcols=160; echo -e "\e[8;$tlins;${tcols}t"; (or echo -e "\e[8;40;160t" ) resizes the window to 160 x 40 characters.

Get Terminal Window Size:
tput lines # return the height in characters (number of lines) of the terminal window
tput cols # return the width in characters (number of columns) of the terminal window
echo "$(tput cols) x $(tput lines)"; # show terminal window size
# The environmental variables $LINES and $COLUMNS contain the values of lines and colums of the terminal window, but their value is not preserved within scripts, so that you'll have to add this line to your script assign them the expected values: COLUMNS="$(tput cols)"; LINES="$(tput lines)";

Open a new Bash shell:
bash

Duplicate a terminal:
apt-get install conspy
conspy N # N can range from 1 to 6, then execute another conspy N with the same number on another bash shell

Show current command line:
cat /proc/self/cmdline # typical output: cat/proc/self/cmdline

Exit from a non login shell:
exit

Show last exit code (exit status):
echo $? # Return the exit status of last command (0: Success (no errors, if the last performed command was executed properly with a clean exit), 1 (or different than 0): Failure). Note that if you execute the command twice, the second time you'll get the exit status of the first execution of echo #?

Exit Codes with special meanings (Reserved Exit Codes):
Exit Code Number: Meaning / Example / Comments:
0: No errors / echo "Hello World" / Command executed successfully. Note that quitting a command with CTRL+D will also return 0.
1: Catchall for general errors / let "var1 = 1/0" / Miscellaneous errors, such as "divide by zero"
2: Misuse of shell builtins / empty_function() {} / Missing keyword or command, or permission problem (and diff return code on a failed binary file comparison).
126: Command invoked cannot execute / /dev/null / Permission problem or command is not an executable
127: "command not found" / illegal_command / Possible problem with $PATH or a typo
128: Invalid argument to exit / exit 3.14159 / exit takes only integer args in the range 0 - 255 (see first footnote)
128+n: Fatal error signal "n" / kill -9 $PPID of script / kill -9 returns 137 (128 + 9) ; CTRL+C returns 130 (128 + 2), CTRL+Z returns 148 (128 + 20).
255*: Exit status out of range / exit -1 / exit takes only integer args in the range 0 - 255

Show environment variables:
printenv # List all environment variables
export -p # List all exported (global scope) variables

Change prompt:
export PS1="[\t] \u@\h:\w\\\$ " # show time in 24 hours format between square brackets, the logged user name, the host name, the current path, and the root status ("#" if root, "$" otherwise)
export PS1="\[\e[1;32m\][\t] \u@\h:\w\\\$ \[\e[0m\] " # same prompt, but colored in light green
export PS1="\[\e[1;32m\][\$(date +%a\ %d%b%Y\ %H:%M:%S)] \u@\h:\w\\\$\[\e[0m\] " # full date and time between square brackets, the logged user name, the host name, the current path, and the root status ("#" if root, "$" otherwise), colored in light green. Note that the shell command date is used, invoked with \$() .  If $() (without the leading backslash) were used, the date would be generated only the first time and then stored into the variable and wouldn't be updated every time the prompt is shown. Note: if you want to set Windows Command Prompt to match your Linux Shell Prompt you have to set the %prompt% environment variable. For example: set prompt=[$d$s$t]$s%username%@%computername%:$p$+$g$s
export PS1="\[\e[1;32m\][\$(date +%Y-%m-%d\ %H:%M:%S\ %a)] \u@\h:\w\\\$\[\e[0m\] " # full date and time (in a sortable format) between square brackets, the logged user name, the host name, the current path, and the root status ("#" if root, "$" otherwise), colored in light green.
echo $PS1 # See current prompt

Run a script locally on the shell

Run a script: php PATH/FILE.php # run a PHP script php -r "echo 'Hello World';"; # Execute a PHP line in the shell php -r 'echo "Hello World\n";'; # Execute a PHP line in the shell. Use quotes to print character sequences. perl -e "print 'Hello World';"; # Execute a Perl line in the shell python -c "print('Hello World')"; # Execute a Python line in the shell python3 -c "print('Hello World')"; # Execute a Python3 line in the shell ruby -e "puts 'Hello World'"; # Execute a Ruby line in the shell




Please DONATE to support the development of Free and Open Source Software (PayPal, Credit Card, Bitcoin, Ether)

Page issued on 25-Sep-2022 04:24 GMT
Copyright (c) 2022 Geody - Legal notices: copyright, privacy policy, disclaimer