'echo dir_path | xargs cd' does not work?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers 'echo dir_path | xargs cd' does not work?
# 1  
Old 04-12-2010
'echo dir_path | xargs cd' does not work?

Hi:

how come this command does not work? the error message is:
xargs: cd: No such file or directory

yet, in the same time, 'echo dir_path | xargs ls' works.

Does it work in bash? We use csh.

Thanks.

NB Phil
# 2  
Old 04-12-2010
No matter what your shell, cd is a shell builtin and not an executable. cd pretty much has to be a builtin; anything else wouldn't be able to change the shell's own current directory. Imagine spawning a process, changing that process' current directory, and waiting for it to return to find that your own current directory remains unchanged -- a /bin/cd program would be useless Smilie

Also, see csh programming considered harmful.
# 3  
Old 04-12-2010
Hi Corona:

Thanks for the response.

we are stuck in csh not by our own choice.

Are you saying that the xargs only supply args to commands or utilities, not to built-ins? Since 'cd' is a built-in, so xargs can't find it?

Thanks.
# 4  
Old 04-12-2010
Absolutely. Xargs is unaware of shell builtins as it launches the command itself and so is independent from any shell.
That wouldn't prevent your script to work on systems like Solaris that have cd as a command too. However, cd is expecting a single argument so there is no much point in using it through xargs.
# 5  
Old 04-12-2010
Thanks for the reply. Did not realize the difference between built-in and commands until now.

Basically, the xargs only searches the commands within the search path.

xargs did not complain about "ls" though. But is "ls" a built-in too?

So I assume there is no way to let xargs search for built-in commands, am i correct?

Thanks.
# 6  
Old 04-13-2010
Quote:
Originally Posted by phil518
xargs did not complain about "ls" though. But is "ls" a built-in too?
"ls" might be a builtin but is always provided as a command.
Quote:
So I assume there is no way to let xargs search for built-in commands, am i correct?
You can always wrap a built-in command with a script.
# 7  
Old 04-13-2010
HP-UX have /usr/bin/cd , but it is a Posix shell script ! This still does not work with xargs so I looked deeper.

After testing with a script not called "cd" it became clear that xargs is passing the parameter correctly BUT the "cd" is executing in a sub shell and not being retained when control returns to the calling shell.

In most Posix shells this construct works because the "cd" is executed in the current shell. Somebody might be able to convert this to csh.
Code:
echo "/new_directory"|while read DIR
do
       cd "${DIR}"
       pwd
done


Last edited by methyl; 04-13-2010 at 09:29 AM.. Reason: layout
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. What is on Your Mind?

Echo eval xargs and other siblings

It always seemed to me that these utils are siblings. All they do are that substitute values for variables, rearrange the parameters, and confuse the input with the output. :) I tried to display their main signature in table together. To show their similarities (3 Replies)
Discussion started by: nezabudka
3 Replies

2. UNIX for Dummies Questions & Answers

Echo does not work to append to a new line

Hi, i am witing a function in a shell script which will echo the file name and witre in to the new line, but i dont get expected results. The below is my code #!/bin/bash DATE=$1 myview(){ base_name=$1 echo -ne "${base_name}${DATE}">> /path/to/file/txt } myview sample3030 myview... (5 Replies)
Discussion started by: vikatakavi
5 Replies

3. Shell Programming and Scripting

echo file deleted by xargs

my command deletes the oldest file from a folder and I'd like to have some type of output when the file is selected or deleted. ls -t -1 | tail -n 1 | xargs rm I'm not sure how to incorporate echo into this. Thanks, (2 Replies)
Discussion started by: evanlivingston
2 Replies

4. Shell Programming and Scripting

echo doesn't work right

Hi,when I run my first shell script,I got something that doesn't work right. I wrote this code in the script. echo -e "Hello,World\a\n"But the screen print like this: -e Hello,World The "-e" wasn't supposed to be printed out. Can anyone help me out?:wall: Many thanks!:) (25 Replies)
Discussion started by: Demon
25 Replies

5. Shell Programming and Scripting

Color on echo output does not work

Hi, When I run: echo "\033I see hi in color, but if I run this color is not shown, why? (echo "\033Thanks Israel. ---------- Post updated at 05:17 AM ---------- Previous update was at 04:43 AM ---------- DONE!! I had to run more -v. Thanks (4 Replies)
Discussion started by: iga3725
4 Replies

6. Shell Programming and Scripting

shell script, echo doesn't work

#!/bin/sh something(){ echo "Inside something" echo $1 $2 } val=$(something "Hello " "world") Output expected: Inside somethingHello world But it's not echoing. (4 Replies)
Discussion started by: cola
4 Replies

7. UNIX for Dummies Questions & Answers

How to correctly use an echo inside an echo?

Bit of a weird one i suppose, i want to use an echo inside an echo... For example... i have a script that i want to use to take users input and create another script. Inside this script it creates it also needs to use echos... echo "echo "hello"" >$file echo "echo "goodbye"" >$file ... (3 Replies)
Discussion started by: mokachoka
3 Replies

8. Shell Programming and Scripting

echo $PWD doesn't work

I have entry in the my .profile like below, but still i see $PWD is not defied in my system export PS1=$LOGNAME@`hostname`':'$PWD'>' echo $PWD also gives me nothing, my env list also give no entry for PWD.Can someone help me setting PWD variable. I use /bin/sh (9 Replies)
Discussion started by: yesmani
9 Replies

9. Shell Programming and Scripting

if [ -z echo foo | egrep -e 'regexp' != '' ] -> dont work

Hallo, I need to test a String (a special ip number-string). So I want to run that: ipadress=172.0.0.0 # for debugging: echo $ipadress | egrep -e '172\.?\.??\.??$' # the test that doesnt work if test -z `echo $ipadress | egrep -e '172\.?\.??\.??$'` != "" then echo "match" else... (1 Reply)
Discussion started by: wiseguy
1 Replies

10. UNIX for Dummies Questions & Answers

bash pattern matching echo *[! '/' ] doesn't work

without using ls, just using echo so purely pattern matching I can say echo */ <-- lists directories but how would I match files? surely something like *!/ or * but neither work ? it seems like there isn't much that I can put in but surely i should be able to put any ascii... (1 Reply)
Discussion started by: james hanley
1 Replies
Login or Register to Ask a Question