Having the following Bash sequence that creates a file, changes file permissions (to be executable) and run the script I want to do in one line the last 2 commands:
$ touch dotfiles.sh
$ chmod +x ./dotfiles.sh
$ ./dotfiles
Solution 1
I thought a possible solution is to use the && operator. So ./dotfiles
will be executed only if chmod
succeeds.
But this solution is not working and bash says the file does not exists. Any idea?.
(not working solution)
Note: chmod
returns 0 if success. So the && is done, but fails in the second part when ./dotfiles.sh
:
$ chmod +x ./dotfiles.sh && ./dotfiles.sh
-bash: ./dotfiles.sh: No such file or directory
$
Update: Solution 1 is correct. See my answer below to full explanation.