Record multi-line result of a bash builtin

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


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?

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 -l 
$ bash

You can get a function to assist:

$ function passdirs() { bash `dirs -l -p`; }
$ cat >
echo $#
echo "[email protected]"
$ passdirs
/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.

2019-05-08 23:18:32

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

2019-05-08 01:32:48