FAQ Database Discussion Community


Getting value from function, use that value to get colored output in shell

shell,sh,zsh,zshrc,oh-my-zsh
(Using zsh) I am just trying to get random color output whenever I open a new tab in my terminal. To achieve this I wrote the following shell script, but it is not working as expected: #Standard Colors red='\033[0;31m' NC='\033[0m' # No Color black='\033[0;30m' blue='\033[0;34m' green='\033[0;32m' cyan='\033[0;36m' purple='\033[0;35m' yellow='\033[1;33m' lgreen='\033[1;32m'...

Simple ZSH function and files with spaces in their name

whitespace,filenames,zsh,expansion
I have put the following function into my ~/.zshrc file: function note() { vim $HOME/Dropbox/$1.md } When I call it with note "20150209-132501-Recx-new note today.md" for example it creates a new file but with the file name "20150209-132501-Recx-new". I realise this is a simple question but how do I get...

read -r : Backslash disappears in zsh, but not in bash

zsh
When I read in raw mode in bash: read -r v now typing 5 characters (quote, backslash, backslash, x, quote): "\\x" and I do a echo $v it displays "\\x" This is what I expect: Because of the -r switch, I get back what I had put in. When I...

How to stop zsh continuing bg job

process,zsh
Observe the following behaviour (which I want to stop happening): > cat /dev/zero [1] + 36461 suspended cat /dev/zero > bg [1] + 36461 continued cat /dev/zero > cat [1] + 36461 running cat /dev/zero I'm not sure why typing cat starts the old cat again, I want it to...

Declaring arrays in ZSH

shell,command-line,zsh
I'm having trouble converting a shell script to zsh. I have the following array defined, but it is throwing the error unknown file attribute: \n. (I'm converting a dotfiles repo to my zsh) declare -r -a FILES_TO_SOURCE=( "bash_aliases" "bash_exports" "bash_functions" "bash_options" "bash_prompt" "bash.local" ) ...

zsh: Command substitution and proper quoting

zsh
I have a script P which accepts file names as parameters: P file1 file2 file3 .... I also have a script G which generates a (typically short list) of file names, one file name per line. In a master script which I would like to write in zsh, I want...

Do not interpolate newline characters in curl response in strings

json,bash,zsh
I'm having a bit of difficulty understanding how to not interpolate escape characters in strings ... particularly those that are returned from a curl response. I am fetching data that looks like: {"foo":"bar\r\nbaz"} On its own, curl resource will print out the above no problem. However, if I use command...

Combining file tests in zsh

zsh
What is the most elegant way in zsh to test, whether a file is either a readable regular file? I understand that I can do something like if [[ -r "$name" && -f "$name" ]] ... but it requires repeating "$name" twice. I know that we can't combine conditions (-rf...

Open file in $EDITOR by default in zsh

zsh
In zsh, if you can open directories without specifying cd command, like this % pwd /home/user/test % ls integration shared unit % unit % pwd /home/user/test/unit This effectively saves writing 'cd' everytime you try to open a directory. However it's smart enough not to 'cd' into files. Would it be...

Using curly braces referencing git revisions/references fails

git,shell,zsh
I am trying to checkout a single file from stash: git checkout [email protected]{0} -- some/file This fails with fatal: invalid reference: [email protected] Looks like the curly braces are eliminated, but I am not sure about the culprit. The stash revision is there. git stash list returns [email protected]{0}: WIP on X...

customizing ZSH tilde expansion

linux,shell,zsh,zsh-completion
Context This concerns ZSH tilde expansion (as in bash tilde expansion). Example: ~http is expanded to /srv/http which is (roughly) the home for user http set in /etc/passwd. What I want to do I would like to configure ZSH in order to get any filename tilde expansion I wish. This...

oh-my-zsh error after upgrade: ~/.oh-my-zsh/lib/misc.zsh:3: parse error near `then'

shell,zsh,zshrc,oh-my-zsh
today I upgrade my oh-my-zsh and then start this error: ~/.oh-my-zsh/lib/misc.zsh:3: parse error near `then' Inside this file there is this shell loop... ## Load smart urls if available for d in $fpath; do if [[ -e "$url/d-quote-magic"]]; then autoload -U url-quote-magic zle -N self-insert url-quote-magic fi done I think...

zsh - Include directories in glob loop

zsh,glob
I have the following zsh script which symlinks all files in a given directory (except README.md) to my home directory with a leading . in front of each file name. setopt EXTENDED_GLOB for rcfile in "${ZDOTDIR:-$HOME}"/.dotfiles/home/^README.md(.N); do ln -fsv "$rcfile" "${ZDOTDIR:-$HOME}/.${rcfile:t}" done The problem is that the glob doesn't seem...

Execute a block of commands without keeping it in shell history (zsh)

zsh
I know you can avoid keeping the command in history if you use setopt HIST_IGNORE_SPACE. But what about a block of commands? Imagine I write something like: nohistorystart ...cmds... nohistoryend Or something that starts a prompt....

ZSH Agnoster Theme showing machine name

shell,ubuntu,zsh,digital-ocean,oh-my-zsh
I have a development server hosted on Digital Ocean, using Ubuntu 14.04. I switched my shell to ZSH and decided to go with the Agnoster theme. In order to get [email protected] to stop showing, I set the DEFAULT_USER in my .zshrc file. For some reason on the ubuntu server, that's...

zsh script to encode full file path

url,zsh
I want to be able to encode a path for use as a url i.e change spaces to %20. I found this function which does the encoding: urlencode() { setopt localoptions extendedglob input=( ${(s::)1} ) print ${(j::)input/(#b)([^A-Za-z0-9_.\!~*\'\(\)- ])/%${(l:2::0:)$(([##16]#match))}} } and want to be able to pass the results of this:...

zsh prompt and hostname

shell,zsh,hostname,zshrc
I use the following prompt in .zshrc: PROMPT="%{$fg[magenta]%}%n%{$reset_color%}@%{$fg[blue]%}%m %{$fg[yellow]%}%1~ %{$reset_color%}%# " When I open terminal I see this prompt: [email protected] ~ % Is it possible to drop the text "zoltan" in the hostname? I would like to make it look like this: [email protected] ~ % Any suggestion would be greatly...

zsh (vs. bash) parameter expansion anomaly

zsh
If you execute the script X=abcd echo ${1:-$X} echo ${1?$X} without passing any parameter, and do it under bash, it outputs abcd ./mrh.sh: line 3: 1: abcd If you run it under zsh, it outputs abcd mrh.sh:3: 1: -$X We see that in the case of ${...?...}, the substitution ($X)...

How to print combined words on terminal?

printing,terminal,zsh
From PHOTO(By on my zsh): I wonder how to print combined word on terminal? it looks so cool. Is there any tool to do this. Anyone knows? THX ...

Append to variable filename in Bash

bash,zsh
Why does this work: echo "foo" >> ~/Desktop/sf-speedtest-output.csv But this does not? outputFile="~/Desktop/sf-speedtest-output.csv" echo "foo" >> $outputFile # Error: No Such file or directory I have tried it in ${}, $(), "". Is this not an escaping issue?...

Removing lines from multiple files with sed command

osx,bash,shell,sed,zsh
So, disclaimer: I am pretty new to using bash and zsh, so there is a chance the answer is really simple. Nonetheless. I checked previous postings and couldn't find anything. (edit: I have tried this in both bash and zsh shells- same problem.) I have a directory with many files...

zsh split directory into array

arrays,zsh
I'm trying to get an array containing the full current directory path in zsh. I'm currently using local pwd="${PWD/#$HOME/~}" pwd_list=(${(s:/:)pwd}) Which works except for one problem, it treats the starting / as a directory split too. I'd like my array to be like / usr lib php instead of usr...

Why is this symbolic link created two instances

bash,zsh
I have an install script used for my dotfiles. I am using to create symbolic links of one directory to my home folder. The links execute fine apart but a second symobolic link is created and I cannot reason why. The folder structure in the project looks like this install.sh...

In zsh how do I bind a keyboard shortcut to run the last command?

keyboard-shortcuts,zsh,oh-my-zsh
I frequently find myself wanting to repeat a command, and while !! is useful, I'd like to bind that to ctrl-w or something like that. Is there a way to do that? EDIT: I'm aware that the up arrow does what I want, however I would rather not have to...

I accidentaly delete zsh from /bin folder in Mac

zsh
I delete the zsh binary from bin folder , so when i try to change my shell i get this : chsh: WARNING: shell '/bin/zsh' does not exist I don't know how to reinstall zsh shell and MacOs come with zsh by default....

Make vim follow symlinks when opening files from command line

linux,osx,vim,zsh
I'm a huge vim lover, but I can't find a way to get vim to follow symlinks when opening files. As an example, all the dotfiles in my home dirs are symlinked to within the .zprezto directory: .vimrc -> ~/.zprezto/runcoms/vimrc .zshrc -> ~/.zprezto/runcoms/zshrc I'm keeping my fork of .zprezto in...

Zsh redirection 2>>|

zsh,options,io-redirection
I came upon this which I am replicating in order to debug a zsh line editor widget, and I realized that I have no actual idea what 2>>| actually does. Presumably feed stderr into a file, but what's the deal with the |?

Using basename to name output file in java

java,find,xslt-1.0,filenames,zsh
I have a recursive structure of directories containing some foo files that I want to convert to bar files using a XSLT 1.0 stylesheet. I have: dir |-- subdir | |-- file1.foo | |-- file2.foo | |-- file3.foo And I want to obtain: dir |-- subdir | |-- file1.foo |...

Oh My Zsh - how to use GitHub plugin?

plugins,github,zsh,zshrc,oh-my-zsh
Just enabled github plugin in .zshrc: plugins=(… guthub …) After this autocomplete works fine, when I press tab I see: But then, if, I try to run actual command, e.g. github create test-repo I'm getting zsh: command not found: github Until now I used simple plugins only with aliases and...

Using ANSI escape sequences in zsh prompt

zsh,zshrc
I'm trying to use ANSI escape sequences to set the color of my zsh prompt, but the escape character (\e) seems to be being escaped when displaying the prompt. Here's an example of what I'm running and the output I'm getting: > autoload promptinit && promptinit > autoload colors &&...

zsh redirection, together with parameter expansion

shell,zsh,io-redirection,variable-expansion
I'm writing a shell script S which calls a program P. P accepts its input from stdin only. One of the parameters of my script S is optional; if the parameter is present, it denotes a file, which should be fed into P. If the parameter is missing, the stdin...

Why shell returned a Python error traceback when I `cd` into python-skeleton dir

python,zsh,cd
I installed Pycharm via HomeBrew cask on my OS X 10.10. I heard python-skeleton is something useful so cded into it to find what does it have. But every time I type cd /opt/homebrew-cask/Caskroom/pycharm/4.5/PyCharm.app/Contents/helpers/python-skeletons/ gets this traceback: Traceback (most recent call last): File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py", line 548, in <module> main() File...

Shell out into zsh and execute commands from bash script

linux,bash,shell,vagrant,zsh
I'm trying my hand a little virtualisation, so I've been using vagrant to provision a centos7 vm and now I am configuring it with applications. My vagrant config runs a bootstrap.sh file which is a bash script, in it I install zsh, then I want to configure it as per...

Facebook PathPicker does not work with ZSH

zsh
I am trying to use Facebook PathPicker with ZSH. When I try to run a command on a selection I get this error: > git status -uno | fpp /root/.oh-my-zsh/oh-my-zsh.sh: line 13: syntax error near unexpected token `(' /root/.oh-my-zsh/oh-my-zsh.sh: line 13: `for config_file ($ZSH/lib/*.zsh); do' /root/.zshrc: line 123: unsetopt: command...

Recursively search directory of binary files for hexadecimal sequence?

shell,zsh,binaryfiles,hexdump,find-util
The current commands I'm using to search some hex values (say 0A 8b 02) involve: find . -type f -not -name "*.png" -exec xxd -p {} \; | grep "0a8b02" || xargs -0 -P 4 Is it possible to improve this given the following goals: search files recursively display the...

Switching to zsh shell — `ls` ends terminal tab

bash,shell,command-line,zsh,fish
I'm switching from Fish Shell to Zsh. I just went through a long process of converting a bunch of my Fish functions into Zsh, and most things are working, but I just noticed that when I type ls, it not only doesn't work, but actually ends the terminal tab session:...

Weird function declaration (or is it?) in zsh

function,shell,zsh,zsh-completion
I tried to find certain files by filename creation in zsh (although I read the manpage multiple times I did not really now what I was doing), but I think something went terribly wrong: After I typed echo 10-02-2015.*(), I was prompted to type something — zsh put out function>....

Can one read input into a element of a associative array?

zsh
I want to use read to put data into a particular element of an associative array, so I wrote the following: typeset -Ag aa aa[key]='initial value' ... read aa[key] which produces an error: no matches found: aa[key] So I write the following instead and it works: typeset -Ag aa aa[key]='initial...

Adding node_modules to repo makes ZSH slow

node.js,zsh
I recently added my node_modules directory to version control per this answer's advice. (By the way, I'm not necessarily of the opinion that adding node_modules to version control is a good practice, but I'm trying to get a deployment working and I don't have anything else to try right now.)...

Shell (zsh) through Emacs?

emacs,zsh
I usually run emacs in a terminal to work. I would like to run a shell inside a emacs window. I've found the shell command, but I would like to run the zsh I usually use inside (with my own configuration). Is it possible? EDIT : I succeeded to run...

Why doesn't find let me match multiple patterns?

osx,bash,zsh
I'm writing some bash/zsh scripts that process some files. I want to execute a command for each file of a certain type, and some of these commands overlap. When I try to find -name 'pattern1' -or -name 'pattern2', only the last pattern is used (files matching pattern1 aren't returned; only...

color schemes interaction in terminal, zsh, and vim

vim,terminal,zsh,color-scheme
I noticed that the color-scheme setting in .zshrc does not affect the background color of the terminal window, so I decided to load a separate preset color-scheme for the OSX terminal. This, in return, changed how the .zshrc color-scheme looked previously. Vim also has its own color-scheme that seems to...

how to pass tmux varible to external script for the status bar

shell,environment-variables,zsh,statusbar,tmux
I am using the default <#{pane_current_path}> to display the current working directory for the current pane used. However the result is a lengthy full directory path taking lot of space in the status bar. What I wanted is to use a shell script or ruby script to cut it short...

xargs: exec command with prompt

redirect,zsh,xargs
I'm trying to do the following with xargs pacman -Q | grep xf86-video | awk '{print $1}' | xargs pacman -R to remove all xf86-video-* driver on my machine. To make the question more clear, here is the output of pacman -Q | grep xf86-video | awk '{print $1}': xf86-video-ark...

Z.sh Directory jumper - Change where history is kept

shell,zsh
Using z in zsh, I want to change the default location of where the history is kept. By default, when your source the shell script source ~/z/z.sh It will make a file with the directories you visit in a .z/ directory. While this is fine for most cases, I want...

ZSH: Behavior on Enter

zsh
I realize, when I'm in my terminal, I would expect to press Enter on empty input to make a ls or a git status when I'm on a git repos. How can I achieve that? I mean, have a custom behavior on Empty input -> Enter in zsh? EDIT: Thanks...

How do I merge two arrays in a zipper like fashion in bash?

arrays,bash,zsh
I am trying to merge two arrays to one in a zipper like fashion. I have difficulty to make that happen. array1=( one three five seven ) array2=( two four six eight ) I have tried with nested for-loops but can't figure it out. I don't want the output to...

strange zsh filtering behavior

zsh,qualifiers
Doing: print -l ./somedir/**/*.{png,jpg} gives expected listing of png and jpg files, and there are both types of them present, BUT print -l ./somedir/**/*.{png,jpg}(Lk+50) does NOT list my jpg files greater than 50k, saying no matches found ./somedir/**/*.png(Lk+50). Why it stops on png here, not expanding on jpg as well?...

Cant run bash script example from “The linux command line, a complete introduction, 2nd”

osx,bash,shell,unix,zsh
Here is an example of my zsh script, that script was modified from original one: #!/bin/sh set -e # read-default.sh: supply a default value if user preses Enter key. read -e -p "What is your user name? " -i $USER echo "You answered: '$REPLY'" Here is an original script from...

Should my $PATH be returning anything RVM related?

ruby,osx,shell,zsh
I am running the command echo $PATH which is returning the following information containing RVM /Users/RAI/.rvm/gems/ruby-2.1.1/bin:/Users/RAI/.rvm/gems/[email protected]/bin:/Users/RAI/.rvm/rubies/ruby-2.1.1/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/RAI/.rvm/bin Should this command only return the following? /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin Note that I am currently using on My Zsh...

zsh inserts extra spaces when performing searches and completion

shell,command-prompt,zsh,prompt,ansi-escape
I have written a small program that emits a command line prompt with some Git info. I use ANSI escape sequences to color it, and it looks something like this: However, whenever I do tab completion or a search, zsh inserts several spaces after the prompt: It seems to be...

Is there a method of getting the number of files in git repository with new/modified/deleted status?

python,git,zsh
I'm writing a Python script that prints a string describing the state of a git repository in the current directory. This is being used for creating a right-sided prompt in zsh. The output will look like the right side of this: [email protected]% ⌷ master(+0, ~0, -0) Right now, I'm trying...

How can I prevent ANSI escape sequences in my Python script from messing with my zsh RPROMPT and cursor positions?

python,zsh,ansi-escape
I've been working on a Python script that generates an RPROMPT for zsh about the state of a git repo in the current working directory. It's invoked in my .zshrc file with: RPROMPT='$(python3 ~/.git_zsh_rprompt.py)' To differentiate it from the other text in my terminal, I've used ANSI escape codes to...

How to emulate global aliases from zsh in bash?

bash,zsh
In zsh, you can define global aliases. For example, alias -g G='| grep' alias -g W='| wc -l' and then use them like this: some_command G some_text W Can global aliases be somehow emulated in bash?...

What does `(.N)` do in this extended glob?

zsh,glob,prezto
The Prezto documentation has the following example script for setting up symlinks: setopt EXTENDED_GLOB for rcfile in "${ZDOTDIR:-$HOME}"/.zprezto/runcoms/^README.md(.N); do ln -s "$rcfile" "${ZDOTDIR:-$HOME}/.${rcfile:t}" done I understand everything in "${ZDOTDIR:-$HOME}"/.zprezto/runcoms/^README.md(.N) up until (.N). What does (.N) mean here? Bonus question, what is ${rcfile:t}? I understand that it resolves to the name...

checkwinsize equivalent in zsh?

bash,shell,zsh,zshrc
I am migrating from bash to zsh and as part of that I am transitioning over my dotfiles. Is there an equivalent to "checkwinsize" in zsh? I couldn't find one after searching, the relevant code in my .bashrc is shopt -s checkwinsize I tried setopt -s checkwinsize to no avail....

zsh: command not found: grunt-init

node.js,gruntjs,zsh,zshrc,grunt-init
I'm new to gruntjs and am trying to get the grunt-init templates up and running however I constantly get this error: zsh: command not found: grunt-init when I run the install it shows me all of this, so it seems like it is installing fine → npm install -g grunt-init...

How to prevent execution of command in ZSH?

linux,zsh
I wrote hook for command line: # Transforms command 'ls?' to 'man ls' function question_to_man() { if [[ $2 =~ '^\w+\?$' ]]; then man ${2[0,-2]} fi } autoload -Uz add-zsh-hook add-zsh-hook preexec question_to_man But when I do: > ls? After exiting from man I get: > zsh: no matches found:...

Can I use sed or something to remove YAML frontmatter from markdown files?

bash,sed,zsh
I have markdown files that contain YAML frontmatter metadata, like this: --- title: Something Somethingelse author: Somebody Sometheson --- But the YAML is of varying widths. Can I use a Posix command like sed to remove that frontmatter when it's at the beginning of a file? Something that just removes...

How do I take the output of recursive grep and split it into an array like variable?

arrays,bash,shell,sed,zsh
I'm trying to create a shell search and replace function to replace all occurrences of a string in a directory. The problem is how do I take the output of recursive grep and use it to find files to use for sed? I've got the following pipeline grep -R protal...

First tab completion enhancement [closed]

zsh,completion
Customizing zsh allows you to simply hit the tab key and let you cycle through directories. See this answer. That is an amazing workflow improvement, but I need help with the following: How can I achieve, that zsh tab completion will show me ALL files and folders and lets me...

zsh “cd..” command stopped working without space (cd..)

zsh,oh-my-zsh
"cd.." stopped working without space. I used to type "cd.." but since the last update only "cd .." works it throws: zsh: command not found: cd.. how can I get it back? thank you....

how to convert an csh to zsh alias that requires arguments

zsh,zshrc
When using csh I use this alias: alias s autossh -M 0 -t \!:1 \"tmux -2 attach -t $USER\!:2 -d \|\| tmux -2 new -s $USER\!:2 \" That can help me to ssh to a remote server by using something like: s 10.11.12.3 X Where X is the suffix $USERX...

Bash Scripting - Obtaining file names from directory

linux,bash,shell,testing,zsh
I am trying to create a zsh script to test my project. The teacher supplied us with some input files and expected output files. I need to diff the output files from myExecutable with the expected output files. Question: Does $iF contain a string in the following code or some...

Shell function command with quotes

bash,shell,command-line-interface,zsh
I am trying to make a git shortcut to commit with a message. Since aliases do not support parameters I came up with this function: function gcm() { git commit -m "[email protected]" } My expectation is to commit with a message without even typing quotes like this: gcm create cli...

zsh: command not found: express even after I have installed both express and express-generator globally

node.js,shell,express,zsh
Hey so when I try to run express in terminal mac it doesnt work. I already installed express-generator and express using sudo npm install -g express and sudo npm install -g express-generator. I was able to find them both in my npm directory. However for some reason, the express command...

Tmux breaks zsh aliases

bash,shell,alias,zsh
I have a file .aliases that I point to in my .zshrc file DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" if [ -f $DIR"/.aliases" ]; then . $DIR"/.aliases" fi This code is the same in my .bashrc file (in the process of switching over to zsh). The aliases...