Tag Info

Hot answers tagged

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


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


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


10

This fork bomb always reminds me of the something an AI programming teacher said on one of the first lessons I attended "To understand recursion, first you must understand recursion". At it's core, this bomb is a recursive function. In essence, you create a function, which calls itself, which calls itself, which calls itself.... until system resources are ...


10

Disclaimer: This answer deals with Bash specifically but much of it applies to the question regarding glob patterns! The star character (*) is a wildcard. There are a certain set of characters that it will take the place of and the first character being a dot (.) isn't one of them. This is a special case just because of how the Unix filesystems work, files ...


9

I have implemented a zsh-autosuggestions plugin. It should integrate nicely with zsh-history-substring-search and zsh-syntax-highlighting which are features ported from fish.


8

Getting a variable to Python Since variable substitution occurs before text is passed from the heredoc to python's standard input, you can throw the variable right in the script. python - <<EOF some_text = "$some_text" EOF If some_text was "test", python would see some_text = "test". If you want to be able to pull your Python code right into a ...


8

In zsh, personal preference. In other shells $array may only expand to the first element, thus ${#array} would output the length of the first element. So, if you want to be little more portable between shells specifying the [@] would work. In zsh, $array expands in the same way $array[*] would, which differs depending on if they appear within quotes or ...


7

In zsh, you can define the hook function preexec to echo a specified string before every command is executed. In bash, you could use the DEBUG trap to set up a preexec hook to do the same. You would define a preexec() function something like this: preexec () { echo "-------------------------------"; } preexec_invoke_exec () { [ -n "$COMP_LINE" ] ...


7

The following will work with GNU find and awk: find /path -type f -printf '%u %k\n' | awk '{ arr[$1] += $2 } END { for ( i in arr ) { print i": "arr[i]"K" ...


7

Add zsh to /etc/shells: command -v zsh | sudo tee -a /etc/shells You can now use chsh to set zsh as shell: sudo chsh -s "$(command -v zsh)" "${USER}" See this documentation: Changing your login shell


6

you want to bind the editor command reverse-menu-complete to the menuselect keymap. bindkey -M menuselect '^[[Z' reverse-menu-complete I am not sure how portable the escape sequence ^[[Z is, so you may want to check terminfo(5) to see if there is a way of using the $terminfo array to correctly bind it. Note that the menuselect keymap is available after ...


6

This is likely caused by your setting of the prompt confusing zsh about where the cursor is in the terminal. You've included terminal control characters in your prompt without letting zsh know that those won't actually show up on the terminal. This needs to be done by surrounding those portions of the prompt with %{ and %}. This has already been done when ...


6

Instead of having a command to enter your thought, you could define a widget for that. In zsh: thought() { print -r -- $BUFFER >> ~/.thoughts # or: your-script $BUFFER BUFFER= zle -M 'thought recorded' } zle -N thought bindkey '^T' thought Then press Ctrl-T to add the content of the buffer to your ~/.thoughts file and clear it afterwards ...


6

The problem is that zsh is globbing the remote path. You can verify this by scp luna4:"/u/paige/maye/src/diviner/notebooks/plots/hk_*" . To turn globbing off for scp remote paths, but otherwise leave globbing the same (from here) add this to your .zshrc - # Disable globbing on the remote path. alias scp='noglob scp_wrap' function scp_wrap { local -a ...


6

You misread that comment. In Bourne-style shells such as bash and zsh, the command set is a builtin which does two things: it sets shell options, e.g. set -x turns on the xtrace (print debugging traces) option; when called with non-option arguments, it sets the positional parameters ($1, $2, …, collectively accessed as "$@"). As an exception, if the ...


6

Alias substitution is only performed when reading lines from interactive sources. So the second alias is not affected by the first, hence the literal replacement. Perhaps something similar: PACMAN=pacman if [ -f /usr/bin/pacmatic ]; then PACMAN=pacmatic fi # Colorized Pacman output alias pacman="${PACMAN} --color auto" This'll set 'pacman' to the ...


5

Linux (you mentioned "only under Ubuntu" but the only OS you mentioned it working under was Darwin) does not support passing multiple arguments to a 'shebang' interpreter. It passes the entire string (in your case, "zsh -") as a single argument. The correct way to ensure your package does not depend on the location of an interpreter is to, as part of the ...


5

Take a look at the push-line ZLE command. This command pushes the current contents of the line onto the buffer stack and clears the line. You can then enter another command and when ZLE starts up again, the line is popped from the buffer stack into the line buffer. prompt@world$ cp abc /mnt/usr/bin # The user presses Esc-q prompt@world$ ...


5

You can use readlink to resolve the symbolic link and then dirname to get its directory. cdl () { cd "$(dirname "$(readlink "$1")")"; } bash-3.2$ pwd /foo/bar bash-3.2$ ls -l total 8 lrwxr-xr-x 1 root wheel 11 Jun 15 19:10 foo.sh -> /bar/foo.sh bash-3.2$ cdl foo.sh bash-3.2$ pwd /bar bash-3.2$


5

Use the scalar PATH form instead of the array path. Making either of these local effectively makes both of them local, so: foo() { local PATH=$PATH if ( some_condition ) path=( $PREFIX $path ) # do stuff } (Note that this will not work if some path component has an embedded :.) Unfortunately, it is not possible to initialize a local array ...


5

You could use the symlinks command to convert absolute paths to relative: /tmp$ mkdir -p 1/{a,b,c} 2 /tmp$ cd 2 /tmp/2$ ln -s /tmp/1/* . /tmp/2$ ls -l total 0 lrwxrwxrwx 1 stephane stephane 8 Jul 31 16:32 a -> /tmp/1/a/ lrwxrwxrwx 1 stephane stephane 8 Jul 31 16:32 b -> /tmp/1/b/ lrwxrwxrwx 1 stephane stephane 8 Jul 31 16:32 c -> /tmp/1/c/ We've ...


5

Zsh doesn't have anything like closures or packages or namespaces. Zsh lacks a bunch of things required to have true closures: Functions aren't first class. You can't pass functions around as arguments to other functions, and functions can't return other functions. (You can pass the name of a function to call, but that's not the same as passing the ...


5

I don't think there's any way to turn it off. First thought is to have preexec set a variable that indicates a command was run. If you pressed Ctrl+C at the prompt, it wouldn't get set. precmd() { exit_status=$? if ! $ran_something; then exit_status=0 fi ran_something=false } preexec() { ran_something=true } ...


5

In normal Bourne-style shells such as the Bourne shell, dash, ksh and bash, the syntax $variable means “take the value of the variable, split it into separate words where characters from IFS appear, and treat each word as a file name wildcard pattern and expand it if it matches one of more file”. If variable is an array, this happens to the first element of ...


5

Use the exec builtin with a redirection but no command to redirect the shell's file descriptors. If you want to restore the original destination, you'll have to save it in another descriptor. exec 3>&1 >foo.txt for x in *; do print $x done exec >&3 3>&- When the scope of the redirection is a portion of code, it is simpler to put ...


5

The easiest way to do this would be to not use links at all. The location of the zsh history file is determined by the value of $HISTFILE. So, to have that backed up, change it from the default value to a file in the watched directory. Add this line to your .zshrc: HISTFILE=~/foo/.zhistory Now, copy the existing file over to the new location and any new ...


5

The zsh/parameter module, which is part of the default distribution, provides an associative array options that indicates which options are on. if [[ $options[extended_glob] = on ]]; then … For options that have a single-letter alias (which is not the case of extended_glob), you can also check $-. Note that it's rarely useful to test which options are ...


4

Use the n glob qualifier. print -lr -- *.mp3(n) You can change the default sort order by setting the numeric_glob_sort option. setopt numeric_glob_sort print -lr -- *.mp3 If you need lexicographic order for one pattern while numeric_glob_sort may be in effect, negate the n glob qualifier. print -lr -- *(^n)


4

If you're using bash or ksh for your shell, you can create a function in one of your dotfiles (.profile, .bash_profile, etc) like so: function hr { echo "--------------------------------------------------------------------------------" } Then, you can prepend it to commands whenever you want that separator using && or ; as command separators. ...



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