rather than write scripts for every single process I need to do, I like to just save commands in a text file and copy and paste them on the command line. Of course, some of the specifics of the commands change each time I paste and run it. Then I need to arrow thru the command and change the important parts. So, I would like to do something like this.

For a simple example:

chmod $1 /home/$2/.bashrc 740 bobsa

Can that be done some way? Is xargs something that can be used for this? I know I can set ENV variables before running the command to attain this but I was hoping from something else I can tac on the end thanks!

  • 1
    Have you considered writing functions in your .bashrc? They take arguments at the end in the same way as any other command – Torin Jul 4 at 16:13
  • What is your example command supposed to do? – Hauke Laging Jul 4 at 23:35
  • I'm wondering the same thing as Hauke Laging. What is that command supposed to do? What are the parameters $1 and $2 representing? – Nasir Riley Jul 5 at 0:26
  • sorry for not being clear. in the end I would hope the command would run as chmod 740 /home/bobsa/.bashrc substituting the values at the end of the command for $1 and $2 – josa Jul 5 at 2:10
  • i know the syntax is not right at all but I was hoping someone might know a syntax/command that I could do this on a single command line. thanks! – josa Jul 5 at 2:16

You can use the bash command with -c flag as follows:

bash -c 'chmod "$1" /home/"$2"/.bashrc' bash 740 bobsa

Remember to use single quotes for the bash command.

| improve this answer | |
  • That should rather be bash -c 'chmod "$1" "/home/$2/.bashrc"' bash 740 bobsa because a process with the name 740 would be rather strange. In this case that would be a very short-lived process but there is no reason for doing it differently. – Hauke Laging Jul 5 at 18:21
  • I always used the version without bash and starting with $0, but I think you have a point. It is probably safer to do as you suggest; I will update my answer. – Giuseppe Clemente Jul 6 at 19:48
  • thanks! exactly what I was hoping for. I tried using bash on the command line but i had no idea of the syntax. thanks again! – josa Jul 7 at 19:16

If you have commands you use often, write them as functions, and store them in the shell's startup files, e.g. .bashrc. That one could be defined like this (though you'd need a better name):

f() {
    chmod "$1" "/home/$2/.bashrc"

and called as

f 740 bobsa

That doesn't work too well if you need to edit the command often on the command line. But even then, you could just drop the function definition on the same command line and immediately run it:

f() { chmod "$1" "/home/$2/.bashrc"; }; f 740 bobsa

With {} you need the spaces and semicolons, but you could also use () instead. They start a subshell but you probably don't mind:

f() (chmod "$1" "/home/$2/.bashrc"); f 740 bobsa
| improve this answer | |
  • thanks this is helpful too! I was thinking something like this might work, and now I have the syntax. i'll use this in the future for sure – josa Jul 7 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.