Alt-u: upper case word starting at cursor
Alt-l: lower case word starting at cursor
Ctrl-k: kill (ie, cut) all characters after cursor
Ctrl-u: kill all characters before cursor
Ctrl-y: paste last killed characters
Ctrl-f: move cursor forward by one character (same as right arrow)
Ctrl-b: move cursor backward by one character (same as left arrow)
Alt-f: move cursor forward by one word (same as Ctrl-right arrow)
Alt-b: move cursor backward by one word (same as Ctrl-left arrow)
https://mastodon.social/@YesIKnowIT/106757814475112522
basename
: file name with extensiondirname
: name of parent directory-n
: not set-z
: is set ('is zero')-f
: check if a file exists$
-sign prefix
$-
: string with set of options in current shell
$@
: all positional arguments$#
: number of positional argumentsFunction args:
$1, $2, …
$0 is still script name
$USER
: current username
$HOME
: home dir of current user
References:
$(dirname /path/to/a/file.txt) # /path/to/a/
Handy for use with cd
command to do stuff like
cd $(dirname $_) # go to directory of last argument of last command
Use -n
option of export
without the
$
in the variable.
export -n <variable>
!!
!-<n>
alias <name>='<command>'
Like
alias emacs='emacs -nw'
unalias <name>
mv path/to/file/{a,b}.ext
is same as
mv path/to/file/a.ext path/to/file/b.ext
Add a space before the command.
for i in {1..10}; do echo $i; done
Reference: https://www.delftstack.com/howto/linux/how-to-concatenate-strings-using-bash/
a="hi"
b="hello"
c=$a$b
echo $c # hihello
a="hi"
c="${a}hello"
echo $c # hihello
a="hi"
b=10
c=$a$b
echo $c # hi10
a="hello";
echo "hi".${a} # hihello
+=
operatora="hi"
b="hello"
a+=$b # hihello
.
in bash is same as the source
command.
As in
. ~/bash_aliases
Reference: https://unix.stackexchange.com/questions/114300/whats-the-meaning-of-a-dot-before-a-command-in-shell
Use $1 for first argument, $2 to 2nd, and so on.
Use ^old^new
to replace occurrence of the string
old
with new
(they needn't be strings
themselves).
fam@ubu:~$ echo hello
hello
fam@ubu:~$ ^l^hehe
echo hehehelo
hehehelo
https://mastodon.social/@YesIKnowIT/107537702786292217
-z
: true if argument string is null-n
: true if argument string is not nullReferences:
man bash
https://stackoverflow.com/questions/1335815/how-to-slice-an-array-in-bash
# Creation
$ arr=(foo bar 1 1.2)
$ echo $arr
foo
$ echo ${arr[@]}
foo bar 1 1.2
# Indexing
$ echo ${arr[1]}
bar
# Slicing
$ echo ${arr[@]:1:2}
bar 1
# Slice from pos 1 to end of array
$ echo ${arr[@]:1}
bar 1 1.2
$ echo ${arr[@]::2}
foo bar
Note that array needs the (
and )
.
files=$(ls -1 workspace/)
is not an arrayfiles=($(ls -1 workspace/))
is an array$ echo $((0x12))
18
$ echo $((0x12 << 2))
72
$ echo $((8#13))
11
$ echo $((8#13 << 2))
44
# last one command
fc
# last 3 commands
fc -3 0
This will open up an editor ($FCEDIT
is looked at first
to select the editor) with the last n commands (default n=1). We can
edit them as we like. When the editor is closed, those commands will be
executed.
# For like "2022-07-04 05:16:19"
HISTTIMEFORMAT="%F %T "
history
Use the variable in .bashrc
to make this permanent.
Reference: https://askubuntu.com/questions/391082/how-to-see-time-stamps-in-bash-history
set -e |
Stop script on error |
---|---|
set -u |
Throw error on unset variable use |
set -o pipefail |
Throw error if any command in a 'pipe-chain' of |
commands fail even if other commands succeeded |
-f
/ -d
: true if file/directory
exists-s
: true if file is non-empty (non-zero size)-r
/ -w
/ -x
: true if file is
readable / writable / executablez
: true if variable is unset or is empty string
-e
: any kind of file-S
: Socket-L
: Symbolic link-b
: block special file-c
: character special fileFrom: https://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash
$
is not uesd for variable name in the
test.a=2
if [[ -v a ]; then
echo "a set"
else
echo "a not set"
fi
if [[ -d /home/user ]; then
echo "/home/user exists"
else
echo "/home/user doesn't exist"
fi
# Or
if [[ ! -d /home/user ]; then
echo "/home/user doesn't exist"
else
echo "/home/user exists"
fi
Replace -d
with -f
for files.
In bash: "$(</path/to/file.txt)"
.
echo "$(</data/2.txt)"
See:
Use type
.
For example, I got a function named csv
:
$ type csv
csv is a function
csv ()
{
if [ $# -lt 1 ]; then
echo "Please specify file name";
else
if [ -f $1 ]; then
column -s, -t < $1 | less -#2 -N -S;
else
echo "$1: File not found";
fi;
fi
}
cat << SAMEWORD
hello
world
SAMEWORD
Reference: https://tldp.org/LDP/abs/html/here-docs.html
-n
: not set-z
: is set ('is zero')if [ -n $var ]; then
echo "var is set"
fi
if [ -z $var ]; then
echo "var is not set"
fi
Reference: https://tldp.org/LDP/abs/html/here-docs.html
if [ -n $var ]; then
echo "var is set"
fi
if [ -z $var ]; then
echo "var is not set"
fi
In a terminal do: C-v <key>
.
Eg:
^[[A |
Up arrow |
^[[D |
Left arrow |
^[[5~ |
Page up |
^[[1;5D |
Ctrl-Left |
The ^[
is an escape sequence or something. The remaining
characters are the real deal.
These codes might be dependent on the terminal being used.
https://stackoverflow.com/questions/11816046/signs-for-the-enter-key-page-up-and-page-down
return
is for stating exit code? Values are 'returned'
via printing. Do an echo
.
cd
is same as cd $HOME
C-x C-v
Sample output:
GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)