From b893c73571dc7e1256f0aec44dd126c5acaec0f6 Mon Sep 17 00:00:00 2001 From: tiyn Date: Thu, 30 Mar 2023 02:23:27 +0200 Subject: [PATCH] shell: different shells and for loops --- wiki/linux/shell.md | 99 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 87 insertions(+), 12 deletions(-) diff --git a/wiki/linux/shell.md b/wiki/linux/shell.md index de09679..5d4b285 100644 --- a/wiki/linux/shell.md +++ b/wiki/linux/shell.md @@ -3,20 +3,95 @@ A shell is a command-line interpreter that provides a command-line interface to interact with the many unix-like system. -## Change the standard shell +## Unix Shells -The Shell in some distributions is made up by a POSIX compliant mode of bash. -Problem with that is the speed. -A shell like dash would be much faster. -This is a guide on how to persistently change the symlink /bin/sh from bash to -dash. +The following is a list of Unix shells that are POSIX compliant. -- `yay -S dash` - Install dash -- `sudo ln -sfT /bin/dash /bin/sh` - link /bin/sh to dash -- `sudo vim /usr/share/libalpm/hooks/bash-update.hook` - setup a hook to avoid -bash resetting the symlink +- [Bash](https://www.gnu.org/software/bash/) is the GNU Projects shell. + It is probably the most used and well known shell. +- [DASH](http://gondor.apana.org.au/~herbert/dash/) is a small implementation of + `/bin/sh`. It is significantly faster than Bash and it is recommende to set + DASH to be [the system shell](#changing-the-system-shell-binsh). +- [Zsh](https://www.zsh.org/) is a shell designed to be interactive but also a + powerful scripting language. It is often set to be the + [default user shell](#changing-the-default-user-shell) because of its user + friendliness and a more features like the possibility for + [syntax highlighting](https://github.com/zsh-users/zsh-syntax-highlighting) + and [auto suggestions](https://github.com/zsh-users/zsh-autosuggestions). -## Disown a Command +## Usage + +This section addresses various different functions by and actions that can be +taken with shell commands. + +### For Loops + +For loops can be especially useful when scripting. +A function could be called on all the files in a directory. +The following command shows the basic functionality and syntax of a for loop in +a POSIX compliant shell. +The command appends `-1` to all files in the current working directory. +It consists of the loop structure that cycles through all files `*`. +and appends `-1` using `mv "$f" "${f}-1"`. +The command aswell as the scope could be change to only cycle through specific +filetypes or use another command. + +```sh +for f in * + do mv "$f" "${f}-1" +done +``` + +This command can also be shortened to a single line by using `;` like the +following shows. + +```sh +for f in *; do mv "$f" "${f}-1"; done +``` + +### Changing the Default User Shell + +The default user shell can be changed with the following command. +Change `` according to the shell to set. +For [ZSH](#unix-shells) this would be `/bin/zsh`. + +```sh +chsh -s +``` + +### Changing the System Shell /bin/sh + +The system shell in some distributions is made up by a POSIX compliant mode of +Bash. +This can be slow at times and a shell like +A shell like [DASH](#unix-shells) would be much faster. +This is a guide on how to persistently change the symlink /bin/sh from Bash to +DASH. +Run the following commands to install and link `/bin/sh` to the DASH shell. + +```sh +yay -S dash +sudo ln -sfT /bin/dash /bin/sh +``` + +Afterwards create the file `/usr/share/libalpm/hooks/bash-update.hook` and fill +it with the following text. + +```txt +[Trigger] +Type = Package +Operation = Install +Operation = Upgrade +Target = bash + +[Action] +Description = Re-pointing /bin/sh symlink to dash... +When = PostTransaction +Exec = /usr/bin/ln -sfT dash /usr/bin/sh +Depends = dash +``` + +### Disown a Command This section is based on a guide by [baeldung](https://www.baeldung.com/linux/detach-process-from-terminal). @@ -25,7 +100,7 @@ Commands that are detached from a shell will not terminate upon its closure. A command can be disowned simply by appending `& disown` at the end of it. It will then provide the PID of the command that got disowned. -## Silencing a Command +### Silencing a Command This section is based on a guide by [baeldung](https://www.baeldung.com/linux/silencing-bash-output).