On 26/11/2013 08:59, [email protected] wrote:
> Hello,
> 
> My Bash skills are not that advanced, so 
> I am wondering if it is possible to nest one command inside in another 
> command, not in a script,but on the command line,for instance
> to copy a file to a different destination while changing permissons at the 
> same time, all in one line.
> 


You don't do it that way. I understand what you want to do, but your
description makes no sense.

How you do it is by running two commands on one line, one after the other.

To copy a file "myfile.txt" to /tmp and also change it's permissions,
use the ";" separator:

cp myfile.txt /tmp ; chmod 644 /tmp/myfile.txt

That runs the first command (cp) and then blindly runs the second one.




Sometimes you want to run the second command only if the first one
succeeds (there's not much point in chmod'ing a file that didn't copy
properly. "&&" does this:

cp myfile.txt /tmp && chmod 644 /tmp/myfile.txt

"&&" is boolean logic and a very common programming trick. I won't bore
you with details - it gets complex and we'd have to deal with brash
crazies like why true and false is the wrong way round the the rest of
the world, but just know it this way:

the second command (chmod) will only run if the first (cp) succeeded. If
it failed, the chmod will not be be tried.

Note that "&&" is definitely not the same thing as just one "&" - that
is something completely different. Bash is full of such stuff, it's all
done deliberately to mess with your head :-)


-- 
Alan McKinnon
[email protected]


Reply via email to