Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture newlines from command substitution output in fish shell?

Tags:

shell

fish

In bash, newline characters are preserved through command substitution:

$ TEST="$(echo 'a
  b
  c
  ')" && echo "$TEST"
# →
a
b
c

However, when I try to do the same in fish shell, the newline characters get converted into spaces:

$ set TEST (echo "a
  b
  c
  "); and echo "$TEST"
# →
a b c

How to make fish save the newline characters as newlines?

like image 945
tomekwi Avatar asked Apr 08 '15 11:04

tomekwi


People also ask

How do you export a variable from fish?

To give a variable to an external command, it needs to be “exported”. Unlike other shells, fish does not have an export command. Instead, a variable is exported via an option to set , either --export or just -x .

How do I change the prompt in fish?

Prompt Tab The "prompt" tab displays the contents of the current fish shell prompt. It allows selection from 17 predefined prompts. To change the prompt, select one and press "Prompt Set!". DANGER: This overwrites ~/.

How do you put a path on a fish shell?

It does this by adding the components either to $fish_user_paths or directly to $PATH (if the --path switch is given). It is (by default) safe to use fish_add_path in config. fish, or it can be used once, interactively, and the paths will stay in future because of universal variables.

Where is the fish config file?

The configuration file runs at every login and is located at ~/. config/fish/config. fish . Adding commands or functions to the file will execute/define them when opening a terminal, similar to .


1 Answers

Conceptually, they are not converted into spaces: you are getting a list!

> echo -e "1\n2\n3" > foo
> cat foo
1
2
3
> set myFoo (cat foo)
> echo $myFoo
1 2 3
> echo $myFoo[0..2]
1 2

Therefore we apply the machinery available for lists; for instance, joining with a separator (note the extra backspace to get rid of undesirable spaces):

> echo {\b$myFoo}\n
1
2
3
 # extra newline here

This is not ideal; string does it better:

> string join \n $myFoo
1
2
3
like image 61
Raphael Avatar answered Nov 03 '22 17:11

Raphael