Tag Info

Hot answers tagged

85

Using & causes the program to run in the background, so you'll get a new shell prompt instead of blocking until the program ends. nohup and disown are largely unrelated; they suppress SIGHUP (hangup) signals so the program isn't automatically killed when the controlling terminal is closed. nohup does this when the job first begins. If you don't nohup a ...


38

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 ...


35

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


32

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 ...


27

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 ...


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

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 ...


22

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 ...


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

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 ...


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

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 ...


17

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 ...


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

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.


15

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 ...


15

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 ...


14

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 ...


14

If your scripts start with the line #!/bin/bash they will still be run using bash, even if your default shell is zsh. I've found the syntax of zsh really close to the one of bash, and I did not pay attention if there was really some incompatibilities. I switched 6 yeras ago from bash to zsh seemlessly.


14

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 ...


14

You only need one eval. [ -n "$ZSH_VERSION" ] && eval ' lss() l -l ${1:-.}/*(s,S,t) laf() l ${1:-.}/.*(.) lad() l -d ${1:-.}/.*(/) lsw() l -ld ${1:-.}/.*(R,W,X.^ND/) ' (note that zsh contrary to bash does support the Bourne function syntax) Or: [ -n "$ZSH_VERSION" ] && . /dev/fd/3 3<< '# End of zsh specific ...


13

In bash, use the HISTCONTROL variable. Set it to HISTCONTROL=ignorespace (or HISTCONTROL=ignoreboth). From now, when you begin a line with a space and it will not be saved in the history. This avoids to include the not-to-be-disclosed-command in some configuration file. Even like that it happens to forget to add the space and then want to go back. To ...


13

OK, I got this. The problem isn't autocd, it's correctall. vim as a command (vim file) doesn't trigger any correction*, but vim in sudo vim is an argument, zsh sees that it's close to the name of a folder in the current directory, and asks if you want to change to that, as files and folders are more common arguments. So the solution is unsetopt correctall ...


13

In zsh $PATH is tied (see typeset -T) to the $path array. You can force that array to have unique values with: typeset -U path And then, add the path with: path+=(~/foo) Without having to worry if it was there already. To add it at the front, do: path=(~/foo "$path[@]") if ~/foo was already in $path that will move it to the front.


12

You can run a command on the remote server without logging in like this: ssh -lUSERNAME SERVER COMMAND e.g. ssh -lsomeuser someserver 'mv .zshrc .zshrc.bak' The command given as last argument to ssh will be executed by a non-interactive shell and commands from .zshrc are only executed by interactive shells (see zsh manpage, section on startup and ...


12

With setopt histignorespace, the command is removed from the current session history. If you tested by pressing Up and seeing that the command line is still there, it's a feature. Note that the command lingers in the internal history until the next command is entered before it vanishes, allowing you to briefly reuse or edit the line. If you want to ...


11

You've already found zle-keymap-select which is executed whenever the mode changes. You could use it to set some other visual indicator than the prompt, depending on what your terminal supports it (and your taste in mode indicator display, of course). There is a standard terminfo capability to change the shape of the cursor. However some terminals display ...


11

If the script properly begins with #!/bin/bash (you can't have another comment before that), you should simply execute it by typing /path/to/script.sh, without that . at the beginning. The . is an include statement, it means “execute the contents of this file as if it had been typed here on the command line” (this is called “sourcing” in unix shell jargon). ...



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