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.
  • 38,596
  • 16
  • 96
  • 104
Alexander
  • 1,098
  • 3
  • 16
  • 36

2 Answers2

1

If your files have only one extension:

$ echo "a.in" | cut -d '.' -f1
a
ForceBru
  • 41,233
  • 10
  • 61
  • 89
1

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
  • 111,130
  • 12
  • 123
  • 212