Help understanding a simple command in BASH


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help understanding a simple command in BASH
# 1  
Old 03-06-2011
Help understanding a simple command in BASH

This is the command. Assume file1 exists but file2 does not:
ls file1 file2 >newfile 2>&1

This simply makes a text file with two lines: file1 \n file2 could not be found. What I don't understand is that when you run this command: ls file1 file2 >newfile, it prints "file2 could not be found" to the screen but stores "file1" in newfile. So my question is what exactly does 2>&1 do and how does this explain the difference between these two commands:
ls file1 file2 >newfile 2>&1
ls file1 file2 >newfile
# 2  
Old 03-06-2011
2>&1 will redirect standard error to wherever you pointed standard output (i.e. the screen by default) to - but in your case, you wrote it to newfile.

ls will print errors to standard error, by redirecting it to the same place (newfile) as standard output the error output will also go to the file (newfile)

Code:
ls > newfile      # write the output of ls to newfile, errors will go to the screen
ls > newfile 2>&1 # write the output of ls to newfile, copy standard error to standard output, which also goes to newfile

This User Gave Thanks to Scott For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Understanding the difference between individual BASH login scripts

Hello... and thanks in advance for reading this or offering me any assistance I'm trying to understand specific differences between the various login scripts... I understand the differences between interactive vs non-interactive and login vs non-login shells... and that's not where my question... (4 Replies)
Discussion started by: bodisha
4 Replies

2. UNIX for Dummies Questions & Answers

Understanding bash code

I am not able to understand below line in unix bash shell.Could anyone explain what it will do result="${path1}/*${var1}*${var2}*wssreligibleitem*.csv" path1 is defined and it is a directory path var1 is defined and it holds string value like abc var2 is defined and it holds string value like... (6 Replies)
Discussion started by: vamsi.valiveti
6 Replies

3. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

4. UNIX for Dummies Questions & Answers

Another Simple BASH command I don't understand. Help?

I have a text file called file1 which contains the text: "ls -l" When I enter this command: bash < file1 > file1 file1 gets erased. However if I enter this command: bash < file1 > newfile the output from "ls -l" is stored in newfile. My question is why doesn't file1's text ("ls -l") get... (3 Replies)
Discussion started by: phunkypants
3 Replies

5. Solaris

Understanding 'du' command

Hi I have a questions related 2 commands : 'du' and 'ls'. Why is the difference between output of 'du' and 'ls' cmd's ? Command 'du' : ------------------ jakubn@server1 /home/jakubn $ du -s * 4 engine.ksh 1331 scripts 'du -s *' ---> shows block count size on disk (512 Bytes... (5 Replies)
Discussion started by: presul
5 Replies

6. Shell Programming and Scripting

Understanding a Simple Shell Script

#! /usr/bin/ksh old=$1 new=$2 for file in *.$old ; do mv $file ${file%$old}$new done exit 0 This script i got from the forum. script changes the extension of the files say example a.txt to a.doc b.txt to b.doc c.txt to c.doc d.txt to d.doc this scipt works fine but i am not... (2 Replies)
Discussion started by: vijays3
2 Replies

7. Shell Programming and Scripting

understanding mv command

hi i was moving a file from one directory to another with the following cmmand mv /home/hsghh/dfd/parent/file.txt . while doing so i i accidently mv /home/hsghh/dfd/dfd . although i gave ctrl c and terminate the move command some of the file are missing in the parent directory and... (1 Reply)
Discussion started by: saravanan71184
1 Replies

8. Shell Programming and Scripting

understanding the sed command

Guys, I am trying to understand the sed command here. adx001 $ a=/clocal/dctrdata/user/dctrdat1/trdroot/recouncil adx001 $ b=`echo $a | sed 's/\//\\\\\//g'` adx001 $ echo $b \/clocal\/dctrdata\/user\/dctrdat1\/trdroot\/recouncil The sed command i took it from the script. Please... (3 Replies)
Discussion started by: mac4rfree
3 Replies

9. Shell Programming and Scripting

SUPER simple bash script to repeat a command...

I need to repeat this command on a configurable interval: igal -a -r -U -w 6 I tried this: #!/bin/bash igal -a -r -U -w 6 sleep 30 Just a guess that it MIGHT work. Can anyone point me in the right direction? -R (6 Replies)
Discussion started by: robfindlay
6 Replies

10. UNIX for Dummies Questions & Answers

Need help understanding script command

We use a UNIX-based system (Lawson) at work and I was given this command to request a data extract from the db admin. The only thing I really understand is the last line as it appears to be joining the files created from the first three lines into one. Is there anyone who can help me breakdown the... (4 Replies)
Discussion started by: KGee
4 Replies
Login or Register to Ask a Question