Echo: Multiple commands

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.

bublitz

Posted 2018-08-24T10:37:10.450

Reputation: 1

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?

– Kamil Maciorowski – 2018-08-24T10:41:27.457

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 – 2018-08-24T10:44:57.840

1First 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 – 2018-08-24T10:50:52.597

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 – 2018-08-24T10:53:33.913

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. – Kamil Maciorowski – 2018-08-24T11:13:56.847

Please see this: What is wrong with echo $(stuff) or echo \stuff``?

– Kamil Maciorowski – 2018-08-27T21:51:47.167

Answers

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.

Kamil Maciorowski

Posted 2018-08-24T10:37:10.450

Reputation: 38 429

-1

I found the solution:

(cd .. && echo `pwd`)

Thank you :)

bublitz

Posted 2018-08-24T10:37:10.450

Reputation: 1

4Why are you still using echo? Just do ( cd .. && pwd ) – Attie – 2018-08-24T10:53:30.113

2

Related: Cargo Cult Programming.

– Kamil Maciorowski – 2018-08-24T10:53:53.707

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 – 2018-08-28T07:03:24.980