bashreplaceterminalone-liner

Bash - How to string replace part of the last argument of the last command when using '>' to redirect the output stream


in bash I have got something like this:

I want to reuse input_file_123.txt to name the output file but replace let's say "input" with "output".

I found this: How can I recall the argument of the previous bash command?,

tried this:

but it says "substitution failed".

I would need it to be a one-liner I can use within a jupyter notebook.

Thx for your help!


Solution

  • One option:

    # store the input file in a variable
    
    $ infile='input_file_123.txt'
    
    # use parameter substitution to change 'input' to 'output'
    
    $ <cmd> <arg1> "${infile}" > "${infile//input/output}"
    

    Not sure I understand the 'one-liner' requirement but fwiw:

    $ infile='input_file_123.txt'; <cmd> <arg1> "${infile}" > "${infile//input/output}"