Command-line shell: Difference between revisions

From ArchWiki
(→‎Input and output: move sponge note from Core utilities#iconv here)
(→‎Input and output: generalize sponge note)
Line 74: Line 74:
See also [[GregsWiki:BashGuide/InputAndOutput|GregsWiki]] and [http://www.tldp.org/LDP/abs/html/io-redirection.html I/O Redirection].
See also [[GregsWiki:BashGuide/InputAndOutput|GregsWiki]] and [http://www.tldp.org/LDP/abs/html/io-redirection.html I/O Redirection].


Unlike [[sed]], ''iconv'' does not provide an option to convert a file in place. However, {{ic|sponge}} from the {{pkg|moreutils}} package can help:
* Redirections truncate files before commands are executed: {{bc|''command'' ''file'' > ''file''}} will therefore not work as expected. While some commands ([[sed]] for example) provide an option to edit files in-place, many do not. In such cases you can use the {{man|1|sponge}} command from the {{Pkg|moreutils}} package.
 
$ iconv -f WINDOWS-1251 -t UTF-8 ''foobar''.txt | sponge ''foobar''.txt
 
See {{man|1|sponge}} for details.
 
* Because ''cat'' is not built into the shell, on many occasions you may find it more convenient to use a [[Wikipedia:Redirection (computing)|redirection]], for example in scripts, or if you care a lot about performance. In fact {{ic|< ''file''}} does the same as {{ic|cat ''file''}}.
* Because ''cat'' is not built into the shell, on many occasions you may find it more convenient to use a [[Wikipedia:Redirection (computing)|redirection]], for example in scripts, or if you care a lot about performance. In fact {{ic|< ''file''}} does the same as {{ic|cat ''file''}}.
* POSIX-compliant shells support [http://tldp.org/LDP/abs/html/here-docs.html Here Documents]:
* POSIX-compliant shells support [http://tldp.org/LDP/abs/html/here-docs.html Here Documents]:

Revision as of 15:32, 30 August 2018

From Wikipedia:

A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems. Users direct the operation of the computer by entering commands as text for a command line interpreter to execute or by creating text scripts of one or more such commands.

List of shells

Shells that are more or less POSIX compliant are listed under #POSIX compliant, while shells that have a different syntax are under #Alternative shells.

POSIX compliant

These shells can all be linked from /usr/bin/sh. When Bash, mksh and zsh are invoked with the sh name, they automatically become more POSIX compliant.

  • Bash — Bash extends the Bourne shell with command-line history and completion, indexed and associative arrays, integer arithmetic, process substitution, here strings, regular expression matching and brace expansion.
https://www.gnu.org/software/bash/ || bash
  • DASH — Descendant of the NetBSD version of the Almquist SHell (ash). A fast POSIX-compliant shell that aims to be as small as possible.
http://gondor.apana.org.au/~herbert/dash/ || dash
  • KornShell (ksh) — The KornShell language is a complete, powerful, high-level programming language for writing applications, often more easily and quickly than with other high-level languages. This makes it especially suitable for prototyping. ksh has the best features of the Bourne shell and the C shell, plus many new features of its own. Thus ksh can do much to enhance your productivity and the quality of your work, both in interacting with the system, and in programming. ksh programs are easier to write, and are more concise and readable than programs written in a lower level language such as C.
http://www.kornshell.com || See the article.
  • Zsh — Shell designed for interactive use, although it is also a powerful scripting language. Many of the useful features of Bash, ksh, and tcsh were incorporated into Zsh; many original features were added. The introductory document details some of the unique features of Zsh.
https://www.zsh.org/ || zsh
Tip: POSIX and Bash scripts can be linted with shellcheck.

Alternative shells

  • C shell (tcsh) — Command language interpreter usable both as an interactive login shell and a shell script command processor. It includes a command-line editor, programmable word completion, spelling correction, a history mechanism, job control and a C-like syntax.
http://www.tcsh.org || tcsh
  • Elvish — Elvish is a modern and expressive shell, that can carry internal structured values through pipelines. This feature makes possible avoiding a lot of complex text processing code. It features an expressive programming language, with features like exceptions, namespacing and anonymous functions. It also has a powerful readline which checks the syntax while typing, and syntax highlighting by default.
https://elvish.io || elvishAUR
  • fish — Smart and user-friendly command line shell. Fish performs full-color command line syntax highlighting, as well as highlighting and completion for commands and their arguments, file existence, and history. It supports complete-as-you-type for history and commands. Fish is able to parse the system's man pages in order to determine valid arguments for commands, allowing it to highlight and complete commands. Easy last-command revision can be done using Alt-Up. The fish daemon (fishd) facilitates synchronized history across all instances of fish, as well as universal and persistent environment variables.
http://fishshell.com/ || fish
  • Nash — Nash is a system shell, inspired by plan9 rc, that makes it easy to create reliable and safe scripts taking advantages of operating systems namespaces (on linux and plan9) in an idiomatic way.
https://github.com/NeowayLabs/nash || nash-gitAUR
  • Oh — Unix shell written in Go. It is similar in spirit but different in detail from other Unix shells. Oh extends the shell's programming language features without sacrificing the shell's interactive features.
https://github.com/michaelmacinnis/oh || oh-gitAUR
  • Powershell — PowerShell is an object-oriented programming language and interactive command line shell, originally written for and exclusive to Windows. Later on, it was open sourced and ported to Mac OS X and Linux.
https://github.com/PowerShell/PowerShell || powershellAUR
  • rc — Command interpreter for Plan 9 that provides similar facilities to UNIX’s Bourne shell, with some small additions and less idiosyncratic syntax.
http://doc.cat-v.org/plan_9/4th_edition/papers/rc || 9base
  • xonsh — A retrocompatible shell based on the python interpreter.
http://xon.sh/ || xonshAUR

Changing your default shell

After installing one the above shells, you can execute that shell inside of your current shell, by just running its executable. If you want to be served that shell when you login however, you will need to change your default shell.

To list all installed shells, run:

$ chsh -l

And to set one as default for your user do:

$ chsh -s full-path-to-shell

where full-path-to-shell is the full path as given by chsh -l.

If you now log out and log in again, you will be greeted by the other shell.

Configuration files

To autostart programs in console or upon login, you can use shell startup files/directories. Read the documentation for your shell, or its ArchWiki article, e.g. Bash#Configuration files or Zsh#Startup/Shutdown files.

See also Wikipedia:Unix shell#Configuration files.

/etc/profile

Upon login, all Bourne-compatible shells source /etc/profile, which in turn sources any readable *.sh files in /etc/profile.d/: these scripts do not require an interpreter directive, nor do they need to be executable. They are used to set up an environment and define application-specific settings.

Input and output

See also GregsWiki and I/O Redirection.

  • Redirections truncate files before commands are executed:
    command file > file
    will therefore not work as expected. While some commands (sed for example) provide an option to edit files in-place, many do not. In such cases you can use the sponge(1) command from the moreutils package.
  • Because cat is not built into the shell, on many occasions you may find it more convenient to use a redirection, for example in scripts, or if you care a lot about performance. In fact < file does the same as cat file.
  • POSIX-compliant shells support Here Documents:
$ cat << EOF
one
two
three
EOF
  • Shell pipelines operate on stdout by default. To operate on stderr(3) you can redirect stderr to stdout with command 2>&1 | othercommand or, for Bash 4, command |& othercommand.
  • Remember that many GNU core utilities accept files as arguments, so for example grep pattern < file is replaceable with grep pattern file.

See also