gdi2290
By:
gdi2290

How do I reuse previous arguments in reverse order in Bash?

March 13, 2015 892 views
Linux Commands Git Linux Basics

How do I do this in one line reusing and reversing the arguments? Also what's the syntax to create a function that will do this?

mv newname.js oldname.js
git mv oldname.js newname.js

for example to make a folder then cd into it you

mkdir new-folder-name && cd $_

is it something like
mv newname.js oldname.js && git mv $2 $1

3 comments
  • have you considered writing a script? It is fairly simple, and you can add variables when you run it.

    just create a text file, save it with whatever you want to name it, and then make it executable with:

     chmod +x   file-name
    

    a quick example of a script, which I can pass two variable to when I run it:

    #!/bin/bash
    
    echo $1
    echo $2
    
    exit 0
    

    now if I run that script like this:

    file-name   "first variable"  "second variable"
    

    it should echo:

    first variable
    second variable

  • thanks, I made the file github.com/gdi2290/git-mv on github

  • awesomer.

    One more fun step: you can make a line in your .bashrc file (if you dont have a .bashrc file, just create a file called .bashrc in your users home directory)

    add this:

    
    #run script
    alias funscriptname="/your/script-file"
    

    reload your bashrc:

    . ~/.bashrc
    

    and now you can run this:

    funscriptname  "variable one"  "variable two"
    
1 Answer

This question was answered by @sierracircle:

have you considered writing a script? It is fairly simple, and you can add variables when you run it.

just create a text file, save it with whatever you want to name it, and then make it executable with:

chmod +x   file-name

a quick example of a script, which I can pass two variable to when I run it:

#!/bin/bash

echo $1
echo $2

exit 0

now if I run that script like this:

file-name   "first variable"  "second variable"

it should echo:

first variable
second variable

View the original comment

Have another answer? Share your knowledge.