Tag Info

Hot answers tagged

55

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


22

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


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

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


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


12

you can put it into parantheses like (cp a b && mv b c && rm a )& to include the whole chain.


12

Use printf builtin: $ printf %s\\n localhost:8080/reports/{promos,promo-updates,scandown}/{130,139,142}{,-unburdened,-burdened}{,.pdf,.xls,.xlsx,.csv,.preload} localhost:8080/reports/promos/130 localhost:8080/reports/promos/130.pdf localhost:8080/reports/promos/130.xls localhost:8080/reports/promos/130.xlsx localhost:8080/reports/promos/130.csv ...


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.


9

Create .bash_profile in your home directory and add these lines: export SHELL=/bin/zsh exec /bin/zsh -l Update: .profile may work as a general solution when default shell is not bash. I'm not sure if .profile may be called by Zsh as well that it could go redundant but we can do it safely with a simple check: export SHELL=/bin/zsh [ -z "$ZSH_VERSION" ] ...


8

This is happening because cut is outputting NULL characters in the output. You can't pass a program arguments which contain a null character (see this). In bash this works because bash can't handle NULL characters in strings, and it strips them out. Zsh is a bit more powerful, and it can handle NULL characters. However when it comes time to pass the string ...


8

The thousands separator is a GNU extension that zsh doesn't support, and it has its own printf builtin that you end up using instead. As mentioned in the linked post, you can get the locale-dependant thousands separator with: zmodload zsh/langinfo echo $langinfo[THOUSEP] If you need to use zsh specifically and exclusively, you can use that with sed. ...


7

With zsh, it's just: mypath=$0:A Now for other shells, though realpath() and readlink() are standard functions (the latter being a system call), realpath and readlink are not standard command, though some systems have one or the other or both with various behaviour and feature set. As often, for portability, you may want to resort to perl: abs_path() { ...


7

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


7

For ls, use -A instead of -a. from man ls: -A, --almost-all do not list implied . and ..


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


7

One difference between the two is that aliases are only a shell feature. Environment variables are inherited by all subprocesses (unless deliberately cleared). The environment variable would be more likely to work even if less is launched indirectly, such as via another shell (e.g. tcsh), man, vim, psql, etc.


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

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


6

For years I've used something similar to deal with the various locations of Bash on systems that I needed my scripts to run. Bash/Zsh/etc. #!/bin/sh # Determines which OS and then reruns this script with approp. shell interp. LIN_BASH="/bin/sh"; SOL_BASH="/packages/utilities/bin/sun5/bash"; OS_TYPE=`uname -s`; if [ $OS_TYPE = "SunOS" ]; then ...


6

The probably closest syntax to that is: p_out='/some/path' arg_1='5' my_script


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

Try: autoload predict-on predict-on See: info zsh --index-search=predict-on for details.


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



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