LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-03-2007, 03:10 AM   #1
colonel_t
LQ Newbie
 
Registered: Apr 2007
Posts: 1

Rep: Reputation: 0
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
 
Old 04-03-2007, 07:02 AM   #2
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Rep: Reputation: 30
Try

Code:
#!/bin/bash
echo "$*" >&2
Hope this helps!
 
Old 01-20-2011, 02:22 PM   #3
mikemcdonough
LQ Newbie
 
Registered: Jan 2005
Location: North Carolina
Distribution: FC4
Posts: 6

Rep: Reputation: 0
old but I thought I would make a correction

Code:
#!/bin/bash
echo $* 1>&2
 
0 members found this post helpful.
Old 07-11-2011, 02:12 PM   #4
lineus
LQ Newbie
 
Registered: Jul 2011
Posts: 1

Rep: Reputation: Disabled
$ 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 )
 
Old 07-12-2011, 02:16 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
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
 
Old 01-02-2014, 02:57 PM   #6
fyiman
LQ Newbie
 
Registered: Jan 2014
Location: Lombard Illinois (Chicago)
Posts: 1

Rep: Reputation: Disabled
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 View Post
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
 
Old 03-03-2014, 09:58 AM   #7
zoogar
LQ Newbie
 
Registered: Mar 2014
Location: China
Distribution: Ubuntu
Posts: 1

Rep: Reputation: Disabled
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
 
  


Reply

Tags
bash, echo, stderr


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH scripting I/O redirection error blinux1 Programming 13 03-18-2008 09:21 PM
Bash Scripting - echo command question gbhil Programming 2 04-27-2005 08:27 PM
BASH scripting error Darklight451 Linux - General 4 11-22-2004 04:59 PM
How do I echo to both standard out and to a file? Br. Nicholas Linux - General 11 10-12-2004 04:09 PM
bash scripting - suppress error messages? brian0918 Programming 2 06-19-2003 12:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration