1

I am trying to print out the output displayed from a command passed into a bash script. The problem I am trying to solve is how to get the output to look exactly like it would if you ran the command from the shell. For example, when I run ls, I see different colors for directories vs. files.

Here is some sample code of what I have so far:

#!/bin/bash

command="$@"
output=`$command`
echo "$output"

So my shell script takes in a command, runs the command, then prints the output. I know that I can customize the color of the output using color codes and echo -e, but I want the output to look just as it does when I run the command from the shell. Any idea of how I can do this?

3 Answers 3

0

If all you need is to display the output, you can run the command inline within your script (just let it write to stdout directly, without storing its output in some variable).

That is, you can replace:

output=`$command`
echo $output

with:

$command

or

eval $command

If you also need that output for some kind of processing, that would be a bit tricky. You can (for instance) use | tee /var/tmp/some-temp-file.$$ and then read the output from the temporary file.

Sign up to request clarification or add additional context in comments.

Comments

0

Some programs, such as ls, check whether standard output isatty() and behave differently depending on that. If you are capturing the command's output in a variable, the shell redirects its standard output to a pipe which is not a TTY.

There is not much you can do about this except reading the manual page for each individual command to find out whether it supports special options that make its behavior independent of whether its standard output is piped. For ls in particular, you can use the dir program that will always produce human-friendly column formatted output as an alternative.

On a more general level: What you are trying to do seems to be a rather strange thing anyway. I'm sure there is a more robust solution to do what you are trying to accomplish.

2 Comments

For the ls program and want the color output, if you're lucky, your ls supports the --color option. So output=$(ls --color); printf %s "$output" will do the trick. To check how your program behaves when plugged into a pipe, you can always issue: mycommand | cat.
The thing I am trying to do is to make it fast and simple to execute a command in multiple directories/projects. For example, if I want to run a 'git status' command on 8 different projects to see what changed locally, I don't want to have to run the command in 8 different directories. The main commands I will be using are git and gradle commands, so I will look at those specifically. The code above and the 'ls' command are just simplified versions of a bigger problem I am trying to solve.
0

Why not just have your script as:

#!/bin/bash
"$@"

It will run any command line passed as argument and print the output unmodified.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.