Questions tagged [zsh]

Zsh is a shell with many advanced command-line and scripting features.

Filter by
Sorted by
Tagged with
176 votes
1 answer
76k views

When is double-quoting necessary?

The old advice used to be to double-quote any expression involving a $VARIABLE, at least if one wanted it to be interpreted by the shell as one single item, otherwise, any spaces in the content of $...
kjo's user avatar
  • 15k
72 votes
1 answer
13k views

What is the difference between the "...", '...', $'...', and $"..." quotes in the shell?

Sometimes I see shell scripts use all of these different ways of quoting some text: "...", '...', $'...', and $"...". Why are there so many different kinds of quote being used? Do ...
Michael Homer's user avatar
151 votes
3 answers
34k views

Have backticks (i.e. `cmd`) in *sh shells been deprecated?

I've seen this comment many times on Unix & Linux as well as on other sites that use the phrasing "backticks have been deprecated", with respect to shells such as Bash & Zsh. Is this ...
slm's user avatar
  • 366k
30 votes
3 answers
14k views

Which shell interpreter runs a script with no shebang?

Suppose the default shell for my account is zsh but I opened the terminal and fired up bash and executed a script named prac002.sh, which shell interpreter would be used to execute the script, zsh or ...
7_R3X's user avatar
  • 1,164
97 votes
7 answers
35k views

How to use `which` on an aliased command?

Like most users, I have a bunch of aliases set up to give a default set of flags for frequently used programs. For instance, alias vim='vim -X' alias grep='grep -E' alias ls='ls -G' The problem is ...
Adrian Petrescu's user avatar
115 votes
4 answers
39k views

How do you use the command coproc in various shells?

Can someone provide a couple of examples on how to use coproc?
slm's user avatar
  • 366k
107 votes
11 answers
8k views

Remember a half-typed command while I check something

I often find myself in the following position: I've started typing a long command at the bash prompt, but half-way through I find out I need to check something with another command. This is a problem ...
Lauritz V. Thaulow's user avatar
63 votes
6 answers
19k views

What features are in zsh and missing from bash, or vice versa?

As a Linux user, I've always just used bash because it was the default on every distro I used. People using other Unix systems such as BSD seem to use other shells far more frequently. In the ...
Macha's user avatar
  • 3,780
43 votes
2 answers
16k views

Is there a reason why the first element of a Zsh array is indexed by 1 instead of 0?

From my experience with modern programming and scripting languages, I believe most programmers are generally accustomed to referring to the first element of an array as index 0 (zero). I'm sure I've ...
deekin's user avatar
  • 610
89 votes
4 answers
25k views

Is there any way to keep a command from being added to your history?

Is there any way to keep a command from being added to your history? I have a command that I want to keep out of my history file, and I really don't care to have it there when I search the history ...
xenoterracide's user avatar
16 votes
1 answer
22k views

Wrapping a command that includes single and double quotes for another command

I recently learned about watch, but am having trouble making it work with relatively sophisticated commands. For example, I would like to ask watch to run the following command on zsh every three ...
Amelio Vazquez-Reina's user avatar
178 votes
3 answers
73k views

Why ZSH ends a line with a highlighted percent symbol?

I've noticed this on occasion with a variety of applications. I've often thought it was because the output was cancelled early (ctrl+c, for example) or something similar, and zsh is filling in a new ...
jktravis's user avatar
  • 2,176
105 votes
9 answers
58k views

How can I detect if the shell is controlled from SSH?

I want to detect from a shell script (more specifically .zshrc) if it is controlled through SSH. I tried the HOST variable but it's always the name of the computer which is running the shell. Can I ...
stribika's user avatar
  • 5,404
76 votes
8 answers
117k views

Making zsh default shell without root access

I am using my school's computers and would like to use zsh instead of bash. I'd like to make it the default shell, but I cannot run a command such as $ chsh -s $(which zsh) because I don't have admin ...
goodcow's user avatar
  • 913
56 votes
3 answers
12k views

How can I expand a quoted variable to nothing if it's empty?

Say I have a script doing: some-command "$var1" "$var2" ... And, in the event that var1 is empty, I'd rather that it be replaced with nothing instead of the empty string, so that the command ...
muru's user avatar
  • 71.3k
22 votes
1 answer
4k views

How does a fork bomb work?

WARNING DO NOT ATTEMPT TO RUN THIS ON A PRODUCTION MACHINE In reading the Wikipedia page on the topic I generally follow what's going on with the following code: :(){ :|:& };: excerpt of ...
slm's user avatar
  • 366k
29 votes
6 answers
41k views

key bindings table?

Do we have a key bindings table that translates all the various ways of referring to a key press? I'm using zsh, but I presume that if there was such a table it would work for any shell. The reason I ...
Ray Andrews's user avatar
  • 2,165
19 votes
1 answer
2k views

How to use associative arrays safely inside arithmetic expressions?

A few Bourne-like shells support associative arrays: ksh93 (since 1993), zsh (since 1998), bash (since 2009), though with some differences in behaviour between the 3. A common use is for counting ...
Stéphane Chazelas's user avatar
90 votes
6 answers
45k views

How to edit command line in full screen editor in ZSH?

In bash, using vi mode, if I hit Esc,v, my current command line is opened in the editor specified by $EDITOR and I am able to edit it in full screen before 'saving' the command to be returned to the ...
Murali Suriar's user avatar
26 votes
1 answer
7k views

Allowing comments in interactive zsh commands

It might be useful to allow comments in zsh commands written on the command line, as in bash, but % echo test # test zsh: bad pattern: # Any way to get the same behaviour as in the bash shell?
Toothrot's user avatar
  • 3,315
24 votes
3 answers
10k views

What is word splitting? Why is it important in shell programming?

I'm getting confused about the role word splitting plays in zsh. I have not been exposed to this concept when programming in C, Python or MATLAB, and this has triggered my interest of why word ...
Amelio Vazquez-Reina's user avatar
1 vote
2 answers
3k views

Using putty, Left and Right keys move cursor one word, instead of one char

I have looked for a solution to this OhMyZSH! problem for a few days now, but can't find a solution. Admittedly, I do not understand how zle really works, same for bindkey. Same goes for the way ...
Gunee's user avatar
  • 123
79 votes
3 answers
83k views

How to get rid of "No match found" when running "rm *"

Using zsh, I get a "No match found" message when choosing a pattern that does not fit with rm and that even when redirecting the output. # rm * > /dev/zero 2>&1 zsh: no matches found: * ...
user123456's user avatar
  • 4,948
60 votes
15 answers
19k views

Do we have more history for cd?

cd - can move to the last visited directory. Can we visit more history other than the last one?
Tim's user avatar
  • 100k
26 votes
3 answers
27k views

Escaping quotes in zsh alias

Following on from this question about stripping newlines out of text, I want to turn this into a zsh alias as follows: alias striplines=' awk " /^$/ {print \"\n\"; } /./ {printf( \" %s \",$0);}"' I'...
Seamus's user avatar
  • 3,573
9 votes
1 answer
1k views

zsh I/O redirection: redirecting and then piping; what is going on?

Can somebody explain what is going on in this command: echo foo >&2 | grep foo Zsh (5.0.2) prints foo twice. One seemingly to stdout and one to stderr, since running these commands: { echo ...
Zorawar's user avatar
  • 835
7 votes
2 answers
1k views

What is 'ls quote'?

Today I happened to press ' after ls So the command is ls' > ls' quote> ls' zsh: command not found: ls\nls > Can someone tell me, what is the ls quote thing?
octref's user avatar
  • 195
465 votes
3 answers
283k views

What should/shouldn't go in .zshenv, .zshrc, .zlogin, .zprofile, .zlogout?

I'm looking for guidelines on what one should and should not include in the various startup files for zsh. I understand the order of sourcing of these files, and the conditions under which they are ...
kjo's user avatar
  • 15k
117 votes
14 answers
131k views

How can I search history with text already entered at the prompt in zsh?

In zsh, I know that I can search history with Ctrl+r. However, oftentimes I start to type a command directly at the prompt, but then realize I should be searching history. When I hit Ctrl+r, it brings ...
Sean Mackesey's user avatar
58 votes
12 answers
40k views

make my zsh prompt show mode in vi mode

I use bindkey -v (for bash-ers set -o vi I think that works in zsh too) or vi(m) mode. but it bugs me that I don't have any visual cue to tell me whether I'm in insert mode or command mode. Does ...
xenoterracide's user avatar
45 votes
2 answers
22k views

Are there problems with hyphens in functions, aliases, and executables?

In my testing (in Bash and Z Shell), I saw no problems with defining functions or aliases or executable shell scripts which have hyphens in the name, but I'm not confident that this will be okay in ...
iconoclast's user avatar
  • 9,147
40 votes
7 answers
40k views

Double and triple substitution in bash and zsh

Follow-up to the background part in this question. In bash I can use ${!FOO} for double substitution, in zsh ${(P)FOO}. In both, the old-school (hack-y) eval \$$FOO works. So, the smartest and most ...
Profpatsch's user avatar
  • 1,789
24 votes
1 answer
4k views

What is the difference between $path and $PATH (lowercase versus uppercase) with zsh?

In FreeBSD 12, using the zsh shell, I noticed this difference when looking at $path (lowercase) versus $PATH (uppercase). echo $path /sbin /bin /usr/sbin /usr/bin /usr/local/sbin /usr/local/bin /...
Basil Bourque's user avatar
22 votes
3 answers
12k views

PS1='$(pwd)' why this works and why is this different from PS1=$(pwd)

Why when I enter this command the prompt changes to my directory? PS1='$(pwd)' I am using single quotes, which means no interpolation , a.k.a echo '$(pwd)' ——→ $(pwd) Furthermore, say that we ...
HashWizard's user avatar
16 votes
5 answers
8k views

List X random files from a directory

Is there a way to list a set of say, 30 random files from a directory using standard Linux commands? (in zsh) The top answer described here does not work for me (sort does not recognize the option -R)...
Amelio Vazquez-Reina's user avatar
9 votes
1 answer
2k views

Sometimes my zsh messes the end of lines

I have this strange issue, that I cannot find the source of, nor a solution to solve. "Sometimes" my zsh gets messed up and start display end of lines in a funny way: As you can see, the newlines ...
nobe4's user avatar
  • 379
3 votes
1 answer
2k views

zsh: Why do I get suspended background processes even when I have `stty -tostop`?

I like my background processes to freely write to the tty. stty -tostop is already the default in my zsh (I don't know why, perhaps because of OhMyzsh?): ❯ stty -a |rg tostop ...
HappyFace's user avatar
  • 1,543
120 votes
5 answers
153k views

How to define and load your own shell function in zsh

I am having a hard time defining and running my own shell functions in zsh. I followed the instructions on the official documentation and tried with easy example first, but I failed to get it work. ...
Amelio Vazquez-Reina's user avatar
91 votes
1 answer
142k views

Command history in Zsh

How does Zsh keep track of command history? I see two files in my home directory: .zhistory and .zsh_history. What is the difference between them? The files seem to contain all of my history. Does ...
Amelio Vazquez-Reina's user avatar
83 votes
3 answers
23k views

ESC key causes a small delay in terminal due to its Alt+ behavior

My terminal setup is gnome-terminal + tmux + zsh with vi bindings. In applications like vim or even in the zsh's command line vi editing mode, I need to frequently hit the ESC key but there is a ...
sharat87's user avatar
  • 4,219
50 votes
3 answers
22k views

what is the zsh equivalent of bash's export -f

So I started using zsh. I like it all right. It seems very cool and slick, and the fact that the current working directory and actual command line are on different lines is nice, but at the same time, ...
ixtmixilix's user avatar
  • 13.1k
37 votes
1 answer
3k views

Avoiding "BASH-isms" in shell scripts

Does there exist a tool similar to Perl::Critic that will inspect your shell scripts and point out flaws, portability issues, uses of non-standard programs without fallbacks, depreciated program uses, ...
amphetamachine's user avatar
37 votes
1 answer
27k views

Dynamic zsh autocomplete for custom commands

I'm trying to write completion functions for some custom functions I wrote, but seem to be really struggling with even the most basic ones. An example function is: function eb_instances() { if [...
zsquare's user avatar
  • 385
27 votes
4 answers
11k views

What is the equivalent of bash indirect referencing ${!FOO} in zsh?

${!FOO} performs a double substitution in bash, meaning it takes the (string) value of FOO and uses it as a variable name. zsh doesn’t support this feature. Is there a way to make this work the same ...
Profpatsch's user avatar
  • 1,789
20 votes
4 answers
3k views

Path independent shebangs

I have a script that I want to be able to run in two machines. These two machines get copies of the script from the same git repository. The script needs to run with the right interpreter (e.g. zsh). ...
Amelio Vazquez-Reina's user avatar
19 votes
5 answers
19k views

Pasting from clipboard to vi-enabled zsh or bash shell

I'd like to be able to paste from the system clipboard (or text selection) into my "vi-like" shell prompt using the keyboard. I normally use zsh and sometimes bash. In both cases, I have the shell set ...
Dalker's user avatar
  • 1,592
11 votes
2 answers
2k views

`time echo` got no output

I'm using Mac, and I'm trying to time the command execution. If I do time echo it doesn't have any output But If I do time ls it does give me the output of time function Any idea why that ...
songyy's user avatar
  • 253
10 votes
3 answers
5k views

mv * folder (avoiding 'cannot move' error) [duplicate]

Anytime I want to move thousands of files to a new folder, I always encounter the same problem. > mkdir my_folder > mv * my_folder mv: cannot move 'my_folder to a subdirectory of itself '...
Amelio Vazquez-Reina's user avatar
10 votes
1 answer
2k views

What is the best way to detect (from a script) whether software is installed?

I've been frustrated before with differences in output from the which command across different platforms (Linux vs. Solaris vx. OS X), with different shells possibly playing into the matter as well. ...
iconoclast's user avatar
  • 9,147
6 votes
2 answers
3k views

Refer to an item in `dirs`

I use pushd to work with multiple directories in bash and zsh. I've aliased dirs to dirs -v so that I get an ordered list when I want to see what's on the directory stack: chb$ dirs 0 /Volumes/...
chb's user avatar
  • 614

1
2 3 4 5
10