0

Hoi everybody,

I am currently struggling with having to execute multiple commands in an echo. The following line is an example of the problem I am having:

echo (cd .. && pwd)

The idea is, that, when I am currently in the folder "home/Documents", the above code prints "home" - but is still in the directory "home/Documents". However, the above command fails.

The more general question is: How can I execute multiple commands in an echo and print the last result (or all results if its not possible any other way).

Thank you and kind regards.

6
  • 3
    Whatever you're trying to do here, I'm almost sure echo is not the right tool. I remember your previous question, you seem to abuse echo. Please see XY problem. What are you really trying to do with this contraption? Commented Aug 24, 2018 at 10:41
  • The other thread is the actual problem - so first sudo su, then lftp and then multiple echo commands - but I think the real problem lies within the echo - that's why I asked this question in the hope, that it also answers the other one.
    – bublitz
    Commented Aug 24, 2018 at 10:44
  • 1
    First of all, that syntax is wrong. Neither bash, nor zsh can parse that. How did you get it to run? echo (cd .. && pwd) bash: syntax error near unexpected token cd'
    – Fanatique
    Commented Aug 24, 2018 at 10:50
  • You are too hung up on my example - I posted that to clarify the problem I have. Next time better try to solve it your own way instead of criticizing the example. Below the solution I found :-)
    – bublitz
    Commented Aug 24, 2018 at 10:53
  • 2
    "instead of criticizing the example" -- People don't understand your code (here and in the other question). You may believe your code is self-explanatory, but it's not. Sorry, it's so wrong and hairy it triggers WTF moments. No shame in this, we all started knowing nothing. In my opinion you need to learn the tools you use and understand them. We can help you with this. Asking "what's wrong with echo `pwd`?" is a good start. Commented Aug 24, 2018 at 11:13

2 Answers 2

2

The idea is, that, when I am currently in the folder home/Documents, the above code prints home - but is still in the directory home/Documents

You don't need echo at all because pwd prints what you want. Use this:

(cd .. && pwd)

There are two smart things here:

  • (whatever) runs whatever in a subshell. If cd is inside these parentheses, it will change the current working directory of the subshell, not the main (your current) shell.
  • a && b runs b iff a succeeded (returned exit status 0). In general, if you want your script using cd to be robust, it's good to always check if cd succeeded. This prevents running other command(s) in a wrong directory.

Note when there are symlinks involved, you may not get the path you expect. See this community wiki answer for details.

-1

I found the solution:

(cd .. && echo `pwd`)

Thank you :)

3
  • 4
    Why are you still using echo? Just do ( cd .. && pwd )
    – Attie
    Commented Aug 24, 2018 at 10:53
  • 2
    Related: Cargo Cult Programming. Commented Aug 24, 2018 at 10:53
  • Yes - you need it. The problem was, that the command needs to be executed on a server and thats why I need to print the echo (so that the server sees the command "pwd" and not the current folder - which would be an invalid command)
    – bublitz
    Commented Aug 28, 2018 at 7:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .