DEV Community

LG
LG

Posted on

Bash tips

NOTE : This is a live document , will be updated in the future !


### Useful commands :

Recently, I needed a template of English alphabet to test some combinatorics ideas utilising Regex, I said I need a line of z-to-z , although be honest I could not find on Google as a first match : don't do this, better take advantage of interactive shell capabilities, I use Git bash for Windows (you may use Bash/Dash as default for your e.g. Ubuntu/Debian respectively), just type the following excluding # abcdefghijklmnopqrstuvwxyz to get the desired result as of mine recent intention i.e. to get letters of English alphabet in ascending order :

 printf "%s" {a..z} # abcdefghijklmnopqrstuvwxyz
Enter fullscreen mode Exit fullscreen mode

# comment_syntax

### Positional parameters :

Depending on the following scenario $0 position variable would target to :

Interactive shell (terminal) Non-interactive shell (file)
/usr/bin/ /source_file[.sh]
set -- -a -b -c # initial condition
echo "$@" # -a -b -c
# to unset all positional parameters at once do the following :
shift N # N is a number starting from 1 for -a, 2 for -b, 3 for -c, etc. if let's say it's first i.e. 1 for -a the N could be omitted, otherwise must be stated e.g.:
shift 1 # same as just shift
echo "$@" # -b -c
# Where is one "BUT" shift commands removes positional parameters with behaviour of dynamic index rather than sparsed index – consider the initial condition i.e.:
set -- -a -b -c # initial condition
shift 2 # would remove -a & -b, not only -b as may be expected resulting not in -a -c, but in probably unexpected of -c only as number 2 would say remove first two of the $@ 
Enter fullscreen mode Exit fullscreen mode

Someone might give me more eloquent example (always welcome leave a comment below) to splice positional parameters in the middle , but for now as I just started learning bash, I see this is quite bearable workaround for interactive shell specifically,e.g. :

# set -- -a -b -c # consider this was initial condition
# NOTE : consider we still on the same bash session
# just reset it with the following :
set -- -a -c
echo "$@" # -a -c 
# I do understand that if those are dozen of positional parameters, that might be an issue or if you want some more dynamic usage of, otherwise for me it does a job, least for now
Enter fullscreen mode Exit fullscreen mode

More is coming soon, stay tuned ! ...

Top comments (0)