0

I have a bash script that accepts file names that end with .in, for example a1.in a2.in, and I want to take that argument and extract the a1 and add .out to it, how do I do that?

I know accepting an argument is $1 - but how do I extract the a1?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Alexander
  • 1,288
  • 5
  • 19
  • 38

2 Answers2

2

To remove a fixed suffix from an argument (or other variable) use ${1%.in} -- that will remove the trailing .in or do nothing if the argument does not end in .in. To add a suffix, just add it: ${1%.in}.out

To remove any suffix, you can use glob patterns after the % like so: ${1%.*}. This will remove the shortest matching suffix. You can remove the longest matching suffix with %%: ${1%%.*}

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
1

If your files have only one extension:

$ echo "a.in" | cut -d '.' -f1
a
ForceBru
  • 43,482
  • 10
  • 63
  • 98