Record multi-line result of a bash builtin

Generally, bash has the ability to assign multi-line result of a command:

L=`ls`

This functions from both interactive shell and also manuscripts. Yet it shows up the result of a builtin can not be recorded in a variable:

L=`dirs -l -p`

This operates in the interactive shell, yet in the manuscript just first line wind up in the variable, possibly as a result of various therapy of builtins by bash.

Why does it take place, and also just how can I do what I require?

0
2019-05-04 15:01:34
Source Share
Answers: 2

There appears to be some obscurity in the manner in which you worded your inquiry. You claimed:

yet in the manuscript just first line wind up in the variable

and afterwards your remarks recommend that you are creating a covering function - not a manuscript.

I believe you recognize that manuscripts are pointless for transforming directory sites, given that any kind of cd within a manuscript does not circulate bent on the premium covering. If without a doubt you are attempting to create your directory site changer in a manuscript, you'll have a bumpy ride as the directory site pile is not acquired by subservient coverings:

$ dirs -l
/home/msw /home/msw/Ubuntu One /home/msw /usr/bin /usr/local /bin /usr/local
$ cat > dirs.sh
dirs -l 
$ bash dirs.sh
/home/msw

You can get a function to assist:

$ function passdirs() { bash ndirs.sh `dirs -l -p`; }
$ cat > ndirs.sh
echo $#
echo "[email protected]"
$ passdirs
8
/home/msw /usr/local /usr/bin /bin /usr/local /usr/bin /bin /home/msw

yet, certainly you would certainly require the function to additionally impact the cd in the existing covering in which instance covering and also unpacking debates via the command line is a pricing estimate job so you are best simply doing it done in the function

$ function cd_pattern() {
   cd $(dirs -l -p | grep --max-count=1 -e "$1") ; dirs
}
$ dirs
~ /usr/local /bin /usr/share/doc/evince ~/Ubuntu One
$ cd_pattern v..c
/usr/share/doc/evince /usr/local /bin /usr/share/doc/evince ~/Ubuntu One

which additionally switched over to making use of $(...) as a basic synonym for backticks to lower pricing estimate complication.

0
2019-05-08 23:18:32
Source

The result of commands can differ relying on whether it is run interactively or within a manuscript. As an example, "multiline result" goes penalty with export :

#! /bin/bash
L=`export`
echo "$L"

, which offers you all the result all the same as when you run it interactively.

Yet not alias, which offers you absolutely nothing.

0
2019-05-08 01:32:48
Source