bashargumentsone-liner

How can I create a file and make it executable without typing the filename twice?


I would like to be able to chmod +x and touch a file in one go without having to list the file name twice as in touch foo && chmod +x foo. Is there a way to do this is in one whack? Here is some pseudo-code explanation describing the expected behavior:

chmod <placeholder> && touch <placeholder> <-redirect- "filename"

I get sick of typing in one off commands and then to go back and execute the same command a few times for different files I have to edit the filename twice in each command. In my psuedo-code example I would only have to return to the previously entered command and edit only the filename string at the end.

I have tried,

touch "$(echo foo && chmod +x !$)"

but the filename is neither at the end of the string, and it doesn't seem to work because the last argument !$ is the one passed to the command before touch.


Solution

  • Like this:

    $ touch foo && chmod +x $_
    

    Now:

    $ ls -l foo
    -rwxrwxr-x 1 me me    0 mars  30 19:16 foo