Bash Special Parameters

Bash Linux

Special parameters are set by the shell to store information about aspects of its current state, such as the number of arguments and the exit code of the last command. Special parameters can only be referenced and cannot have it’s value assigned.

Special parameters are: $*, $@, $#, $$, $!, $?, $0, $-, $_

Parameter Definition
$* List of arguments (as a string)
$@ List of arguments (as an array)
$# Number of positional parameters
$$ PID of the current shell
$! PID of the last command executed in the background
$? Exit code of the last-executed command
$0 Path to the currently running script
$- Current shell option flags
$_ Gives the last argument to the previous command

Atom as Markdown Editor

Markdown

I have been looking for a good Markdown editor for quite a while, and after a lot of research, I finally I found one.

Some of the key features that were very important to me on a Markdown editor were:

  • Live preview
  • File browser (sidebar)
  • GitHub flavored
  • Fast and non laggy preview

And not surprisingly, Atom from GitHub was my solution.

Imgur

You might also want to:

  • Change the preferences of the already installed package markdown-preview to GitHub flavored

apm install markdown-preview-auto-open

Gnome Touchpad Settings Missing in Arch Xps 13

Linux Arch Hardware

I had an issue where the Gnome extension ‘Touchpad Indicator’ stopped working on my xps 13 (Arch). After looking a bit further, it seems that the Gnome Touchpad settings had also stopped working. All I could see was the mouse settings, and the touchpad section was completelly gone.


Solution:

With Gnome 3.20, xf86-input-synaptics is not longer supported, and you should use xf86-input-libinput instead.

You can check what is installed on your Arch system with pacman -Q | grep input. In my case, I had both packages installed:

$ pacman -Q | grep input
inputproto 2.3.2-1
libinput 1.8.2-1
xf86-input-libinput 0.26.0-1
xf86-input-synaptics 1.9.0-1
xorg-xinput 1.6.2-1

Remove xf86-input-synaptics and any configuration file (like /etc/X11/xorg.conf.d/50-synaptics.conf), install xf86-input-libinput and reboot. That should get your configuration working again.

Additional Status for Vim With Powerline

vim bash Linux

Would you like to have more information displayed while reading files in VIM? Powerline is a great utility for that.

Imgur

In it’s default config, it displays:

  • Current mode (normal, insert, visual)
  • Git branch
  • File name
  • File encoding
  • Script type
  • File view percentage
  • Line number

To install it

Arch:

sudo pacman -Ss python-powerline powerline

Add the line below to your ~/.vimrc

set laststatus=2

Note: If you get the error below

Traceback (most recent call last):
  File "<string>", line 9, in <module>
ImportError: No module named powerline.vim
An error occurred while importing powerline module.
This could be caused by invalid sys.path setting,
or by an incompatible Python version (powerline requires
Python 2.6, 2.7 or 3.2 and later to work). Please consult
the troubleshooting section in the documentation for
possible solutions.
If powerline on your system is installed for python 3 only you
should set g:powerline_pycmd to "py3" to make it load correctly.
Unable to import powerline, is it installed?
Press ENTER or type command to continue

Modify either ~/.vimrc (or /etc/vimrc if you want the fix available for multiple users) by adding the line below:

let g:powerline_pycmd = 'py3'

Ubuntu 16.04

Use Python 3

Bash: Parameter Expansion (Substitution)

Bash Linux

Default Usage

${PARAMETER}

Expand parameter value. It can be used for:

  • Separating characters from variable names
    • cd /home/${user}
  • Positional parameter higher than 9
    • echo "Argument 10 is: ${10}"

Indirection

${!PARAMETER}

Expands to the value of the variable named by the value of parameter.

var=var1
var1=temp

$ echo ${!var}
temp

Case Modification

${PARAMETER^}, ${PARAMETER^^} ${PARAMETER,}, ${PARAMETER,,} ${PARAMETER~}, ${PARAMETER~~}

|Operator|Action| |–|————–| |^|Changes first character to upper case| |^^|Changes all characters to upper case| |,|Changes first character to lower case| |,,|Changes all characters to lower case| |~|Inverts case of first character| |~~|Inverts case of all characters|

Variable Name (Prefix) Expansion

${!PREFIX*} ${!PREFIX@}

Expands to a list of all set variable names beginning with the string in PREFIX.

$ echo ${!XDG*}
XDG_CURRENT_DESKTOP XDG_MENU_PREFIX XDG_RUNTIME_DIR XDG_SEAT XDG_SESSION_DESKTOP XDG_SESSION_ID XDG_SESSION_TYPE XDG_VTNR

Substring Removal

${PARAMETER#PATTERN} ${PARAMETER##PATTERN} ${PARAMETER%PATTERN} ${PARAMETER%%PATTERN}

With #, it removes the mathing pattern from the beggining of the variable, where # removes the shortest match, and ## removes the longest.

Note the empty spaces in the example below.

substr="the quick brown fox jumps over the lazy dog"

$ echo ${substr#* }
quick brown fox jumps over the lazy dog

$ echo ${substr##* }
dog

Removing path from a file:

mlog=/var/log/clamav/freshclam.log

$ echo ${mlog##*/}
freshclam.log

The operator % does the same, but at the end of the file.

$ echo ${substr% *}
the quick brown fox jumps over the lazy

$ echo ${substr%% *}
the

Changing the extension of a file

file=123.txt

$ echo ${file%.*}
123

$ echo ${file%.*}.log
123.log

Search and Replace

${PARAMETER/PATTERN/STRING} ${PARAMETER//PATTERN/STRING} ${PARAMETER/PATTERN} ${PARAMETER//PATTERN}

The main diffence is that a single / substitutes the first occurrence, while double // substitute all occurences:

$ echo $substr
the quick brown fox jumps over the lazy dog

$ echo ${substr/the/da}
da quick brown fox jumps over the lazy dog

$ echo ${substr//the/da}
da quick brown fox jumps over da lazy dog

$ echo ${substr/the}
quick brown fox jumps over the lazy dog

$ echo ${substr//the}
quick brown fox jumps over lazy dog

Anchoring

You can use # and % to anchor to the beginning and end respectively

var1=00000000

$ echo ${var1/#0/1}
10000000

$ echo ${var1/%0/1}
00000001

Offset and Lenght

${PARAMETER:OFFSET} ${PARAMETER:OFFSET:LENGTH}

  • Offset removes the amount of characters as specified
  • Lenght prints the specified character lenght after offset

Note: You can also use a negative value for offset and lenght, which will be calculated from the end of the file

$ echo ${substr}
the quick brown fox jumps over the lazy dog
123456789...

Removes first 9 chars

$ echo ${substr:9}
brown fox jumps over the lazy dog

Removes first 3 chars and print the next 5

$ echo ${substr:4:5}
quick

Use Default

${PARAMETER:-WORD} ${PARAMETER-WORD}

If parameter unset (or null if using :), expand to word.

$ my_var=gru
$ echo ${my_var:-yyz}
gru

$ unset my_var
$ echo ${my_var:-yyz}
yyz

Use Alternate Value

${PARAMETER:+WORD} ${PARAMETER+WORD}

If parameter is set (or null if using :), expand to word.

$ my_var=gru
$ echo ${my_var:+yyz}
yyz

$ unset my_var
$ echo ${my_var:+yyz}

Use Default And Assign

${PARAMETER:=WORD} ${PARAMETER=WORD}

If parameter unset (or null if using :), expand to word and assign parameter to the value of word.

my_var=gru

$ echo ${my_var:=yyz}
gru

$ unset my_var
$ echo ${my_var:=yyz}
yyz

$ echo $my_var
yyz

Display Error

${PARAMETER:?WORD} ${PARAMETER?WORD}

If parameter unset (or null if using :), display error with word as appendix, otherwise expand parameter.

my_var=gru

$ echo ${my_var:?Not set}
gru

$ unset my_var
$ echo ${my_var:?Not set}
bash: my_var: Not set

Reference:

code with