Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What I'm looking for is something that would perform a series of commands like this:

# cat file1 >> concated_file
# cat file2 >> concated_file
# cat file3 >> concated_file
# cat file4 >> concated_file

But wouldn't require me to retype the whole command 3 additional times. Maybe just be able to replace the argument from the previous command with syntax like the following:

# cat file1 >> concated_file
# s/file1/file2
# s/file2/file3
# s/file3/file4

I know that if the last argument is the same you could do this:

# cp -p test1.txt test2.txt
# chmod 777 $_
# chown nimmylebby: $_

And you could also use Bash loops like so:

# for f in 'file1' 'file2'; 
> do 
> chmod 750 $f;
> done

However, the above 2 examples are not what I'm looking. I just want to perform a series of commands which only differ by one argument without using for loops. Is this possible? Might not be but hoping it is ;-).

share|improve this question
at least for me it is not clear of what you want to do ... – akira Oct 17 '10 at 14:11
Heh guess it's you + atleast another person. I'll try to add more clarification in the last "looking for" example. – Belmin Fernandez Oct 17 '10 at 14:30
up vote 3 down vote accepted
  • Press Up, edit the command as necessary, press Enter.

  • As previously, but arrange for the changing word to come last in the command for easier edition, e.g., cat >> concated_file file1. This is straightforward with your example command, not so in all cases.

  • Define a function taking the variable part as an argument: f () { cat "$1" >> concated_file; }. Then invoke the function several times: f file1, f file2, …

  • Use history expansion. Here you can do ^file1^file2.

share|improve this answer
Left the best for last huh? History expansion is exactly what I was looking for. Thanks!! – Belmin Fernandez Oct 17 '10 at 15:05
History expansion is awesome, +1 for you. – Wuffers Oct 17 '10 at 15:11

Maybe what you need is a function:

function mycat() { cat $1 >> concated_file; }
mycat file1
mycat file2
mycat file3
mycat file4
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .