46

I was tasked to create an automated server hardening script and one thing that they need is a report of all the output of each command executed. I want to store the error message inside a string and append it in a text file.

Let's say I ran this command:

/sbin/modprobe -n -v hfsplus

The output of running this in my machine would be:

FATAL: Module hfsplus not found

How can I store that error message inside a string? Any help would be greatly appreciated. Thanks!

1
  • I tried running this command: var=$(/sbin/modprobe -n -v hfsplush) And then displaying it: $var But it still doesn't capture the error message inside the string. May 29, 2014 at 7:42

6 Answers 6

53

you can do it by redirecting errors command:

/sbin/modprobe -n -v hfsplus 2> fileName 

as a script

#!/bin/bash
errormessage=$( /sbin/modprobe -n -v hfsplus 2>&1)
echo $errormessage

or

 #!/bin/bash
errormessage=`/sbin/modprobe -n -v hfsplus 2>&1 `
echo $errormessage

if you want to append the error use >> instead of >

Make sure to use 2>&1 and not 2> &1 to avoid the error "syntax error near unexpected token `&'"

5
  • I've tried that approach and it stores it DIRECTLY in the text file. I want it to store inside a string first so I can format the contents easily. May 29, 2014 at 7:45
  • 1
    @MiguelRoque see updates
    – Nidal
    May 29, 2014 at 7:46
  • 1
    I tried putting the output inside a HEREDOC and it worked also. Thanks a lot @Networker! May 29, 2014 at 7:52
  • 1
    Someone reverted the edit I did, because I had a "syntax error near &" and removed the space after >. A justification would have been nice. Jan 12, 2018 at 10:08
  • I tried to edit because : Make sure to use 2>&1 and not 2> &1 to avoid the error "syntax error near unexpected token `&'"
    – peter_v
    Jun 28, 2018 at 8:14
19

Simply to store as a string in bash script:

X=`/sbin/modprobe -n -v hfsplus 2>&1`
echo $X

This can be a bit better as you will see messages when command is executed:

TMP=$(mktemp)
/sbin/modprobe -n -v hfsplus 2>&1 | tee $TMP
OUTPUT=$(cat $TMP)
echo $OUTPUT
rm $TMP
3
  • 1
    Always use $(command) instead of backticks for command substitution. It is better :)
    – Sreeraj
    Feb 13, 2015 at 7:02
  • I know it is better (I've had less issues using $()), but why is that?
    – KolonUK
    Oct 29, 2019 at 11:48
  • @KolonUK For one thing, you can nest them.
    – Nobody
    May 21, 2020 at 14:05
10

To return the error message in a variable, simply;

error=$(/sbin/modprobe -n -v hfsplus 2>&1 1>/dev/null)

echo $error
1
  • 2
    That's the answer, since the other ones would append standard output to the variable. Usually, you want to know if the message comes from a error or not and that's perfect. For example, to throw a warning or to handle a bad situation in a script. Sep 15, 2021 at 20:48
6

Newer bash versions (I.e. bash 4.1+):

$ msg=$(ls -la nofile 2>&1)
$ echo $msg
ls: cannot access nofile: No such file or directory
$ 
4

To append to a file use /sbin/modprobe -n -v hfsplus 2>> filename

4

I capture error like this

. ${file} 2>&1 | {
  read -d "\0" -t 0.01 error
    [ -z "$error" ] || log_warn Load completion ${file} failed: "\n${error}"
}

if source failed, I will capture the error and log it.log_warn is just a simple function.

BTW, I use this in my dotfiles

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .