Shell management: Difference between revisions

From Alpine Linux
(added difference between different approaches and removed headings)
(removed interactive and non-interactive distinction as this applies to both)
 
(7 intermediate revisions by 2 users not shown)
Line 5: Line 5:
Alpine Linux uses [[Busybox]] Ash shell for its default shell. It is a standard POSIX shell derived from Debian Ash variant.  
Alpine Linux uses [[Busybox]] Ash shell for its default shell. It is a standard POSIX shell derived from Debian Ash variant.  


One's ~/.bashrc file (or, alternatively, a different shell alias file) could be considered as a basis, say, for an {{Path|~/.ashrc}} or {{Path|~/.config/ash/ashrc}} file, reviewing it carefully for syntax/cli variants against that of Ash shell.  
One's ~/.bashrc file (or, alternatively, a different shell alias file) could be considered as a basis, say, for an {{Path|~/.ashrc}} file, reviewing it carefully for syntax/cli variants against that of Ash shell. For non-login, interactive shells refer to [[#Setting alias|Setting alias]] section.
 
{{Tip|Use {{pkg|checkbashisms}} script to perform basic checks for the presence of bashisms in scripts and help remove them.}}


=== Setting alias ===
=== Setting alias ===


When using Ash shell, one can’t set aliases directly in the {{Path|~/.profile}} file. Once can set '''ENV''' environment variable in {{Path|~/.profile}} file to refer the aliases file. This method works for both login and non-login shells.  The following steps does not affect existing {{Path|~/.profile}} and uses {{path|~/.config/ash/profile}} file :
For non-login shells, Busybox Ash and other POSIX shells do NOT automatically read a startup file like {{Path|~/.ashrc}}. To ensure that both login and non-login shells work consistently, use '''ENV''' environment variable in {{Path|~/.profile}} to refer {{Path|~/.ashrc}} file.  


# First create the file {{Path|/etc/profile.d/profile.sh}} as follows: {{Cat|/etc/profile.d/profile.sh|if [ -f "$HOME/.config/ash/profile" ]; then
# Edit the {{Path|~/.profile}} as follows: {{Cat|~/.profile|<nowiki>...
. "$HOME/.config/ash/profile"
export ENV="$HOME/.ashrc" </nowiki>}}
fi
# Now aliases can be added in the startup file {{Path|~/.ashrc}} as follows: {{Cat|~/.ashrc|<nowiki># ~/.ashrc: interactive shell configuration for BusyBox Ash
}}
 
# Next create the file {{path|~/.config/ash/profile}} as follows: {{Cat|~/.config/ash/profile|export ENV{{=}}"$HOME/.config/ash/ashrc"
# Custom Aliases
}}
alias ls='ls --color=auto'
# Now aliases can be added in the file {{Path|~/.config/ash/ashrc}}: {{Cat|~/.config/ash/ashrc|<nowiki>alias a=alias
alias grep='grep --color=auto'
alias l='ls -lh'</nowiki>
 
}}
# You may want to put all your additions into a separate file like
# ~/.ash_aliases, instead of adding them here directly.
The following approach uses {{Path|~/.profile}} and works only for login shells:


# Edit the {{Path|~/.profile}} as follows:{{Cat|~/.profile|# Alias
if [ -f ~/.ash_aliases ]; then
if [ -f ~/.ash_aliases ]; then
     . ~/.ash_aliases
     . ~/.ash_aliases
fi}}
fi</nowiki>}}
# Edit {{Path|~/.ash_aliases}} file as follows:{{Cat|~/.ash_aliases|<nowiki>alias a=alias
alias c=clear
alias f=file
alias g=grep
alias l='ls -lh'</nowiki>}}
 
=== Checkbashisms ===
 
Perl based {{ic|checkbashisms}} utility from {{Pkg|checkbashisms}} package can be used to perform basic checks for the presence of bashisms in {{Path|/bin/sh}} scripts and help remove them.


== Available shells ==
== Available shells ==


All popular shells are available in Alpine Linux. Refer to the following list:  
Most of the popular shells are available in Alpine Linux repositories as can be seen from the below list.
 
{| class="wikitable" align="center" style="width:100%; border:1px #0771a6 solid; background:#f9f9f9; text-align:left; border-collapse:collapse;"
* {{Pkg|bash}}
|-style="background:#333333; color:#ffffff; font-size: 1.2em; text-align:center;"
* {{Pkg|zsh}}  
|width="10%" | Name
* {{Pkg|fish}}  
|width="36%" | URL
|Remarks
|-
|{{Pkg|bash}}|| https://www.gnu.org/software/bash/bash.html||The GNU Bourne Again shell
|-
|{{Pkg|zsh}} || https://www.zsh.org/||Very advanced and programmable command interpreter
|-
|{{Pkg|fish}} ||https://fishshell.com/||Modern interactive commandline shell
|-
|{{pkg|dash}} ||http://gondor.apana.org.au/~herbert/dash/||Small and fast POSIX-compliant shell
|-
|{{pkg|oksh}} ||https://github.com/ibara/oksh||Portable OpenBSD ksh, based on pdksh
|-
|{{pkg|loksh}} ||https://github.com/dimkr/loksh||A Linux port of OpenBSD's ksh
|-
|{{pkg|yash}} ||https://magicant.github.io/yash||Yet another shell
|-
|{{pkg|tcsh}} ||https://github.com/tcsh-org/tcsh||extended C-shell
|-
|{{pkg|nsh}} ||https://github.com/nuta/nsh||A command-line shell like fish, but POSIX compatible
|-
|{{pkg|elvish}} ||https://elv.sh||Friendly and expressive Unix shell
|-
|{{pkg|nushell}} ||https://www.nushell.sh||A new type of shell
|-
|{{pkg|murex}} ||https://murex.rocks/||Intuitive, typed and content aware shell
|}


To install any of the above shells, say for eg: {{pkg|bash}} shell: {{Cmd|# apk add {{pkg|bash}} {{pkg|bash-completion}}}}
To install any of the above shells, say for eg: {{pkg|bash}} shell: {{Cmd|# apk add {{pkg|bash}} {{pkg|bash-completion}}}}
Line 75: Line 92:
* [https://git.busybox.net/busybox/tree/shell Ash source code]
* [https://git.busybox.net/busybox/tree/shell Ash source code]
* [https://pubs.opengroup.org/onlinepubs/9799919799/ POSIX standard]
* [https://pubs.opengroup.org/onlinepubs/9799919799/ POSIX standard]
* [https://stackoverflow.com/questions/38024160/how-to-get-etc-profile-to-run-automatically-in-alpine-docker/38025686#38025686 stackoverflow on ash shell]


[[Category:Shell]]
[[Category:Shell]]
[[Category:System Administration]]
[[Category:System Administration]]

Latest revision as of 15:23, 7 August 2025

The default shell used by Alpine Linux is the BusyBox variant of the ash shell. This page explains how to use the default shell and various ways to change the default shell in Alpine Linux.

Ash shell

Alpine Linux uses Busybox Ash shell for its default shell. It is a standard POSIX shell derived from Debian Ash variant.

One's ~/.bashrc file (or, alternatively, a different shell alias file) could be considered as a basis, say, for an ~/.ashrc file, reviewing it carefully for syntax/cli variants against that of Ash shell. For non-login, interactive shells refer to Setting alias section.

Tip: Use checkbashisms script to perform basic checks for the presence of bashisms in scripts and help remove them.

Setting alias

For non-login shells, Busybox Ash and other POSIX shells do NOT automatically read a startup file like ~/.ashrc. To ensure that both login and non-login shells work consistently, use ENV environment variable in ~/.profile to refer ~/.ashrc file.

  1. Edit the ~/.profile as follows:

    Contents of ~/.profile

    ... export ENV="$HOME/.ashrc"
  2. Now aliases can be added in the startup file ~/.ashrc as follows:

    Contents of ~/.ashrc

    # ~/.ashrc: interactive shell configuration for BusyBox Ash # Custom Aliases alias ls='ls --color=auto' alias grep='grep --color=auto' # You may want to put all your additions into a separate file like # ~/.ash_aliases, instead of adding them here directly. if [ -f ~/.ash_aliases ]; then . ~/.ash_aliases fi

Available shells

Most of the popular shells are available in Alpine Linux repositories as can be seen from the below list.

Name URL Remarks
bash https://www.gnu.org/software/bash/bash.html The GNU Bourne Again shell
zsh https://www.zsh.org/ Very advanced and programmable command interpreter
fish https://fishshell.com/ Modern interactive commandline shell
dash http://gondor.apana.org.au/~herbert/dash/ Small and fast POSIX-compliant shell
oksh https://github.com/ibara/oksh Portable OpenBSD ksh, based on pdksh
loksh https://github.com/dimkr/loksh A Linux port of OpenBSD's ksh
yash https://magicant.github.io/yash Yet another shell
tcsh https://github.com/tcsh-org/tcsh extended C-shell
nsh https://github.com/nuta/nsh A command-line shell like fish, but POSIX compatible
elvish https://elv.sh Friendly and expressive Unix shell
nushell https://www.nushell.sh A new type of shell
murex https://murex.rocks/ Intuitive, typed and content aware shell

To install any of the above shells, say for eg: bash shell:

# apk add bash bash-completion

Change default shell

There are various ways to change the default shell in Alpine Linux. You can revert back to ash shell at anytime with the same steps.

Note: After performing the below step, you need to log out and login again for these changes to take effect.

By hand

Edit /etc/passwd manually using an editor of your choice. An example line for a user named user is:

Contents of /etc/passwd

... user:x:1000:1000:user:/home/user:/bin/ash ...

Change /bin/ash to point to the path of a shell from /etc/shells. Take care to not delete/mangle the line, as it would make you unable to log in again. The user should be the user you are changing the default login shell for.

Using chsh command

To use chsh command, install the shadow package:

# apk add shadow

And use chsh:

# chsh username

Now enter the path for the shell you want to use (e.g /bin/zsh) and press Enter to confirm this change. The shell should exist in /etc/shells.

See also