Tag Info

Hot answers tagged

56

Citing the Open Group Specification on Shell Command Languages, specifically this section "2.6.3 Command Substitution" this statement regarding backtick's deprecation is actually false. Both forms of the command substitution, backticks (`..cmd..`) or dollar parens ($(..cmd..)) are still supported in so far as the specification goes. excerpt Command ...


43

which is actually a bad way to do things like this, as it makes guesses about your environment based on $SHELL and the startup files (it thinks) that shell uses; not only does it sometimes guess wrong, but you can't generally tell it to behave differently. (which on my Ubuntu 10.10 doesn't understand --skip-alias as mentioned by @SiegeX, for example.) type ...


39

Do ctrl-A to get to start of line, prepend a hash and press return - this way it will stay in the command history as a comment and you can get back to it with cursor up EDIT: just noticed this is a slightly more elegant variant on your #2 ;)


38

A somewhat faster version of alex's Ctrl+A Ctrl+K (which moves to the front of the line and then cuts everything forward) is to just use Ctrl+U, which cuts backward on bash, and the entire line (regardless of your current position) on zsh. Then you use Ctrl+Y to paste it again


34

To rebuild the cache of executable commands, use rehash or hash -rf. Make sure you haven't unset the hash_list_all option (it causes even fewer disk accesses but makes the cache update less often). If you don't want to have to type a command, you can tell zsh not to trust its cache when completing by putting the following line in your ~/.zshrc¹: zstyle ...


33

Brace expansion is very useful if you have long path names. I use it as a quick way to backup a file: cp /a/really/long/path/to/some/file.txt{,.bak} will copy /a/really/long/path/to/some/file.txt to /a/really/long/path/to/some/file.txt.bak You can also use it in a sequence. I once did so to download lots of pages from the web: wget ...


30

There's already been quite a bit of activity on the topic on other Stack Exchange sites. My experience of switching from bash to zsh, as far as can remember (it was years ago²), is that I didn't miss a single thing. I gained a lot; here are what I think are the simple zsh-specific features that I use most: The zsh feature I most miss when I occasionally ...


28

In ZSH: First set HIST_IGNORE_SPACE in your profile and then prefix the commands you don't want stored with a space. From the man page, the following 3 options can be used to say that certain lines shouldn't go into the history at all HIST_IGNORE_SPACE don't store commands prefixed with a space HIST_NO_STORE don't store history (fc -l) command ...


27

There's checkbashisms. On Debian, it's shipped as part of the package maintainer tools. Test your scripts under dash and posh. Both have a few non-POSIX constructs, but if your script works in both, it's likely to work in most places. (With the caveat that it's difficult to test typical shell scripts as they tend to have a lot of corner cases.) If you ...


24

Here are the criteria I use in my ~/.profile: If one of the variables SSH_CLIENT or SSH_TTY is defined, it's an ssh session. If the login shell's parent process name is sshd, it's an ssh session. if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then SESSION_TYPE=remote/ssh # many other tests omitted else case $(ps -o comm= -p $PPID) in ...


23

Hit Ctrl+A, Ctrl+K to move to the start of line and delete (kill) to the end of it. Then do your research, and when ready to continue hit Ctrl+Y (yank) to put your saved (killed) line back.


23

You can use brace expansions: convert -trim -density 400 this_is_a_very_long_filename_of_my_pdf_file.{pdf,png}


21

In zsh, the function search path ($fpath) defines a set of directories, which contain files that can be marked to be loaded automatically when the function they contain is needed for the first time. Zsh has two modes of autoloading files: Zsh's native way and another mode that resembles ksh's autoloading. The latter is active if the KSH_AUTOLOAD option is ...


20

Here's an actual fix. Add the following to .tmux.conf: set -s escape-time 0


20

Check if the INTERACTIVE_COMMENTS option is set. According to this page, "[...] in interactive shells with the INTERACTIVE_COMMENTS option set, [...] # causes that word and all the following characters up to a newline to be ignored." According to the comments were added later, set -k does exactly the same thing.


20

You can run bindkey with no arguments to get a list of existing bindings, eg: # Enter vi mode chopper:~> bindkey -v # Search for history key bindings chopper:~> bindkey | fgrep history "^[OA" up-line-or-history "^[OB" down-line-or-history "^[[A" up-line-or-history "^[[B" down-line-or-history In emacs mode, the binding you want is ...


20

cp a b && mv b c && rm a & is correct. & has lower precedence than &&. In fact & has lower precedence than anything other than ; and newline: & is in the same syntactic category as ;, the difference being that ; runs the command list in the foreground while & runs it in the background. You can test this for ...


19

It has insert and normal mode (the insert mode is default, and escape for normal mode) but no visual mode. In bash: set -o vi You can run it at the command line for just this session or add it to your .bashrc file. Many programs use readline for input, and you can make any of them use vi-style keybindings by setting up your .inputrc with set editing-mode ...


19

First mistake (→ Q2): IFS='\n' sets IFS to the two characters \ and n. To set IFS to a newline, use IFS=$'\n'. Second mistake: to set a variable to an array value, you need parentheses around the elements: array_of_lines=(foo bar). This would work: IFS=$'\n' array_of_lines=($(my_command)) But I recommend not to mess with IFS; instead, use the f ...


19

a | b connects STDOUT from a and STDIN from b just by using dup/dup2. Both commands are executed in parallel. a =(b) replaces the argument to a with an temporary filename. b will be executed before a as the temporary file needs to be created before it can be passed to a a <(b) replaces the argument to a with an named pipe. a and b run in parallel. This ...


19

You cannot solve this through shebang directly, since shebang is purely static. What you could do is having some »least common multiplier« (from a shell perspective) in the shebang and re-execute your script with the right shell, if this LCM isn't zsh. In other words: Have your script executed by a shell found on all systems, test for a zsh-only feature ...


18

If you're running the command over and over and your shell is bash, the HISTIGNORE variable will do this. Lets say you have secret.server.com that you ssh to, FTP files to, etc. that you don't want any line that mentions secret.server.com saved: HISTIGNORE="*secret.server.com*" You can list multiple patterns with a colon separating them. Make sure sure ...


18

Several forms of complex commands such as loops have alternate forms in zsh. These forms are mostly inspired by the C shell, which was fairly common when zsh was young but has now disappeared. These alternate forms act exactly like the normal forms, they're just a different syntax. They're slightly shorter, but less clear. The standard form for the for ...


18

First, separate zsh from the rest. It's not a matter of old vs modern shells: zsh behaves differently. The zsh designers decided to make it incompatible with traditional shells (Bourne, ksh, bash), but easier to use. Second, it is far easier to use double quotes all the time than to remember when they are needed. They are needed most of the time, so you'll ...


18

Here is a non-exclusive list of what each file tends to contain: Since .zshenv is always sourced, it often contains exported variables that should be available to other programs. For example, $PATH, $EDITOR, and $PAGER are often set in .zshenv. Also, you can set $ZDOTDIR in .zshenv to specify an alternative location for the rest of your zsh configuration. ...


17

Brace expansion comes very handy when creating large directory structures: mkdir -p dir1/{subdir1,subdir2}/{subsubdir1,subsubdir2} This will give you find dir1 -type d dir1 dir1/subdir1 dir1/subdir1/subsubdir1 dir1/subdir1/subsubdir2 dir1/subdir2 dir1/subdir2/subsubdir1 dir1/subdir2/subsubdir2 You could even go one step further and put brace expansion ...


17

The most commonly used commands in the default bash emacs mode, for most commonly used keyboards: Movement Ctrl-p, or Up: previous command Ctrl-n, or Down: next command Ctrl-b, or Left: previous character Ctrl-f, or Right: next character Alt-b: previous word Alt-f: next word Ctrl-a, or Home: begin of command Ctrl-e, or End: end of command Editing ...


17

Add to the following to ~/.inputrc: # Press up-arrow for previous matching command "\e[A":history-search-backward # Press down-arrow for next matching command "\e[B":history-search-forward Explanation ~/.inputrc is the configuration file for GNU readline. Many shells, including bash and tcsh use readline for command line editing. The two lines above will ...


16

The following settings seem to work. The text on the second line disappears if the command line overflows the first line. The preexec function erases the second line before running the command; if you want to keep it, change to preexec () { echo; }. terminfo_down_sc=$terminfo[cud1]$terminfo[cuu1]$terminfo[sc]$terminfo[cud1] PS1_2='[some status]' ...


16

I use it when I want to reduce typing: geany /path/to/file1 /path/to/file2 # versus geany /path/to/file{1,2} Another example: wajig install libpam0g-dev libiw-dev libdb-dev # versus wajig install lib{pam0g,iw,db}-dev



Only top voted, non community-wiki answers of a minimum length are eligible