0

Possible Duplicate:
bash - automatically capture output of last executed command into a variable

For example

In Bash shell

user:/ & echo "Hello"
Hello
user:/ & date
Sat Mar 17 01:48:45 ICT 2012

How to get the "Hello" or "Sat Mar 17 01:48:45 ICT 2012" variables

I mean every output from command bash shell

The purpose of this point, I want to get those variables and do something with its.

Like, when echo Hello I want to reverse string to olleH and then print it out.

Something like this

echo "Hello"
olleH

More Information

In Command Line Interface ***Not in Shell Script

When I type a command echo,date (or whatever command) in the Command Line Interface, The output will show in the next line.

user:/ & echo "Hello" <br>
Hello 

The "Hello" variable is the output of bash shell. So, I want to store output Hello into variable and pass into "Dictionary Script"

The Hello will matched and translated depend on Dictionary Script

Hello = Hi (Just example)

Now Hello translated into Hi, send back to shell and print out

Result

user:/ & echo "Hello" <br>
Hi 

How can I grab the output?

Could I expose the Bash Shell source, edit and re-compile?

Could I grab from the /proc/BASHPID?

Community
  • 1
  • 1
Runicer
  • 71
  • 2
  • 13

1 Answers1

1

Attempt at answering edited question

From your edit, it sounds as if you want to have bash executing commands, but the outputs from bash should be passed to a 'transmogrifier' which changes the output according to your whims.

This is not going to be trivial; I'm not sure it will be usable for more than a few seconds of fun.

At one level, you want an interactive shell with its standard output (and maybe standard error) going to your modifier program:

bash -i 2>&1 | transmogrifier

For basic command line work, this will just about be usable, though line editing (even with just backspace) becomes a little problematic. Command line editing becomes intolerable if transmogrifier is changing what you appear to be typing as you type it. (I tried bash -i 2>&1 | tee, which worked surprisingly well, but tee just copies its input to its output; and I'm not sure why I chose tee instead of cat. An interrupt, though, killed the pipeline; just another thing you'd have to worry about.)

Attempt at answering unedited question

For the Hello, you would be best off using:

var1="Hello"

You could also use:

var1=$(echo "Hello")

For the date, you more or less must use the latter notation:

var2=$(date)

You can now do things with those variables, such as:

$ echo "$var1" | rev
olleH
$

Note that simple prompts such as '$' (for Bash and relatives) or '%' (for C shell and relatives) or '#' (for root) are conventional.


I had decided not to mention the use of backticks, but since mgilson commented about them, they do, indeed, exist, and you may come across them in older scripts, and you should not write them in your own scripts.

The $(...) notation for command substitution, as it is called, is simpler in nested scenarios, such as:

cd $(dirname $(dirname $(which gcc)))/lib

compared with the backtick notation for the same operation:

cd `dirname \'dirname \\\`which gcc\\\`\``/lib

(Don't even think of trying to get that second line into MarkDown for comments! That's why I've added this to the answer.)

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • I believe the poster wants to prank someone and doesn't want the have them explicitly assign their output to variables. Have a look at the last snippet: `echo "Hello"` -> `olleH` – Lev Levitsky Mar 16 '12 at 19:16
  • It is probably also worth noting that in bash, backtics (e.g. `\`command\``) have the same effect as `$(command)` just in case you ever see that in a script somewhere (although I think most people consider backtics to be less readible)... – mgilson Mar 16 '12 at 19:28
  • The question is at least ambiguous, @LevLevitsky, or possibly confused about command output and variables. I answered one way that seemed to make some sense of the ambiguity, though the last request (for `olleH` to appear) could also be dealt with via `rev <<< Hello`, amongst other techniques. – Jonathan Leffler Mar 16 '12 at 19:39
  • It my fault to explain the question not clear That I show is just the example so, Let's me explain again In my first post – Runicer Mar 17 '12 at 19:22