LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash Scripting: Echo to Standard Error (https://www.linuxquestions.org/questions/linux-newbie-8/bash-scripting-echo-to-standard-error-542868/)

colonel_t 04-03-2007 02:10 AM

Bash Scripting: Echo to Standard Error
 
Hi all.

I'm trying to print out all arguments passed to a script on a single line sent to STDERR


My script:

#!/bin/bash
echo $* >&2


Am I missing something? I know that >&2 should send STDERR to whatever is after it, but do I need to specify that the line is an error or something?


Thanks

Centinul 04-03-2007 06:02 AM

Try

Code:

#!/bin/bash
echo "$*" >&2

Hope this helps!

mikemcdonough 01-20-2011 01:22 PM

old but I thought I would make a correction
 
Code:

#!/bin/bash
echo $* 1>&2


lineus 07-11-2011 01:12 PM

$ cat myscript.sh
#!/bin/bash
# not sure how portable this is, but it works on rhel 5.4

echo "$*" >/dev/stderr

$ ./myscript.sh unix is fun
unix is fun
$ ./myscript.sh unix is fun 2>~/testing_dev_stderr
$
$ cat ~/testing_dev_stderr
unix is fun


OR


$ cat myscript.sh
#!/bin/bash
# perhaps a more portable way

echo "$*" >/dev/fd/2

$ ./myscript.sh unix is fun
unix is fun
$ ./myscript.sh unix is fun 2>~/testing_dev_stderr
$
$ cat ~/testing_dev_stderr
unix is fun


I suppose the bottom line is that STDERR should be
available to you in the form of a file ( like just about everything else in unix/linux )

chrism01 07-12-2011 01:16 AM

Code:

#output to stdout aka chan 1
# 2 methods: NB default is stdout (chan 1) anyway ...
echo A
echo A >1

# output to stderr aka chan 2
echo B >2

# output to both
# 2>&1 => output stderr (chan 2)to same chan (&) as stdout (chan 1)
echo C >1 2>&1
echo C 2>&1

# chan 0 is stdin


fyiman 01-02-2014 01:57 PM

I realize this is an old post, but I recently needed this information so I'm posting here for people searching for this in the future.

Quote:

Originally Posted by chrism01 (Post 4412310)
Code:

#output to stdout aka chan 1
# 2 methods: NB default is stdout (chan 1) anyway ...
echo A
echo A >1

# output to stderr aka chan 2
echo B >2

# output to both
# 2>&1 => output stderr (chan 2)to same chan (&) as stdout (chan 1)
echo C >1 2>&1
echo C 2>&1

# chan 0 is stdin


For me, this didn't work:
Code:

echo A >1
echo B >2

What happens for me is the "echo A" is redirected to a file named "1", and the "echo B" is redirected to a file named "2".

After some research and experimentation, I found that this works:
Code:

[~]# cat ./myscript.sh

#!/bin/sh -u

echo "Message A: This to stdout or wherever '1>' redirects."
echo "Message B: This to stdout or wherever '1>' redirects.">&1
echo "Message C: This to stderr or wherever '2>' redirects.">/dev/stderr
echo "Message D: This to stderr or wherever '2>' redirects.">&2

So, running the script I get:
Code:

[~]# #### Discard stdout... only stderr displays
[~]# ./myscript.sh 1>/dev/null
Message C: This to stderr or wherever '2>' redirects.
Message D: This to stderr or wherever '2>' redirects.

[~]# #### Discard stderr... only stdout displays
[~]# ./r.sh 2>/dev/null
Message A: This to stdout or wherever '1>' redirects.
Message B: This to stdout or wherever '1>' redirects.

[~]# #### Discard both stdout and stderr... nothing displays
[~]# ./r.sh 1>/dev/null 2>/dev/null
[~]# ./r.sh 1>/dev/null 2>/dev/null
[~]# ./r.sh >/dev/null 2>&1
[~]#

You can't really test this from the shell command line, because any of these or similar commands will all output to the screen, so it looks like the redirection has no effect:
Code:

[~]# echo A
A
[~]# echo B>&1
B
[~]# echo C>&2
C
[~]# echo D 2>&1
D
[~]# echo E>/dev/stderr
E
[~]#

If you put those commands in a script and test it like the "./myscript.sh" example shown above, you should see if it will work for you.


Here is some information about my system:
Code:

[~]# uname -srvmpio
Linux 3.2.45 #4 SMP Wed May 15 19:43:53 CDT 2013 x86_64 x86_64 x86_64 GNU/Linux

[~]# ls /bin/sh /dev/stdout /dev/stderr
lrwxrwxrwx 1 root root  4 Jul 18 23:18 /bin/sh -> bash
lrwxrwxrwx 1 root root 15 Jun 29  2013 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 Jun 29  2013 /dev/stdout -> /proc/self/fd/1


zoogar 03-03-2014 08:58 AM

The above posts are helpful. Thank you!
 
I've just been seaching for a way to echo to stderr without disturbing the normal output (stdout).
And finally I got here.

Here's my testing results:
Code:

$ cat ./stderr_example.sh
#!/bin/bash

if [ -z "$1" ]; then
    echo "error '>&2': argument 1 is empty." >&2
    echo "error '>/dev/fd/2': argument 1 is empty." >/dev/fd/2
    echo "error '>/dev/stderr': argument 1 is empty." >/dev/stderr
    exit 1
fi

echo "argument 1 is \`$1'"
$ var1=$(./stderr_example.sh arg1)
$ echo "var1=[$var1]"
var1=[argument 1 is `arg1']
$ var1=$(./stderr_example.sh)
error '>&2': argument 1 is empty.
error '>/dev/fd/2': argument 1 is empty.
error '>/dev/stderr': argument 1 is empty.
$ echo "var1=[$var1]"
var1=[]

All of the above three forms of echoing messages to stderr meet my need.

Some of my system information:
Code:

$ uname -srvmpio
Linux 3.2.0-59-generic-pae #90-Ubuntu SMP Tue Jan 7 23:07:06 UTC 2014 i686 i686 i386 GNU/Linux

$ ls -lgo --time-style=iso /bin/sh /dev/stdout /dev/stderr
lrwxrwxrwx 1  4 12-28 22:26 /bin/sh -> bash
lrwxrwxrwx 1 15 03-03 21:12 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx 1 15 03-03 21:12 /dev/stdout -> /proc/self/fd/1



All times are GMT -5. The time now is 05:30 AM.