Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 03-01-2008, 02:36 PM   #1
LQ Newbie
Registered: Jan 2006
Posts: 5

Rep: Reputation: 0
Understanding `ls /home |sed '/$user/ d' in Bash

I have the following Bash Script:



for user in $excl_users; do
  if [ "`ls /home|grep $user`" ];then
      echo `ls /home | sed '/$user/ d'`
I would expect that the result of this would be a list of the directories in /home with the exception of $user. However, in reality the result is a complete list of directories in the home directory. If I type,

ls /home|sed '/beeboo/ d'
at the command line I see a listing of all directories in /home with the exception of beeboo.

Does anyone have any insight into this behaviour?
Old 03-01-2008, 02:50 PM   #2
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
A) join the first and second lines.
B) A variable in single quotes isn't expanded. Use single quotes around the part before the variable and the part after.
C) Since ls | sed will output to stdout, you don't need to use echo


for user in $excl_users; do
  if [ "`ls /home|grep $user`" ];then
      ls /home | sed '/'$user'/ d'
It would also be better to include more in the pattern in the sed command. Imagine that you have one user named smith and another named smithy. ./progname smith would delete both.

If you look at the pattern of the output of ls, it would be like
sed '/^'$user'$/d'

If $user has the value of "smith", then this will expand to:
sed '/^smith$/d'
The "$" after smith matches the end of line. The "^" matches the beginning.

Last edited by jschiwal; 03-01-2008 at 03:04 PM.
Old 03-01-2008, 02:54 PM   #3
LQ Newbie
Registered: Jan 2006
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks that worked

Ah, that worked very. Thank you.


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 script, understanding variables antis Programming 5 11-15-2007 05:27 AM
Basic help understanding home wireless network Prostetnic_Jeltz Linux - Wireless Networking 3 10-13-2007 07:21 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM
Setting a home network, need a little understanding.. araldit Linux - Networking 2 01-31-2005 11:53 AM
bash - sed/tr??? pk21 Programming 2 09-05-2003 08:56 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:45 AM.

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