0

I want to assign a file into a variable inside a shell script; the file name would come from command line argument. For example I want to do something like this:

#!/bin/bash
file1 = $1
file2 = $2

Then I want to read file1 line by line using the variable file1, but every time I get an error saying permission denied. How do I access the file using the variable name?

ad absurdum
  • 19,498
  • 5
  • 37
  • 60
Nobody
  • 1
  • Remove the spaces before and after `=`s and enclose the command line arguments in double quotes, i.e. `file1="$1"` – fpmurphy Feb 17 '21 at 06:54
  • @fpmurphy Actually you don't need to quote the right-hand side of an a assignment. (It doesn't hurt, but it's unnecessary.) – tripleee Feb 17 '21 at 07:07
  • @tripleee. Depends, what if the filenames include spaces, newlines, etc? While it is recommended that filenames be limited to the Portable Character Set, it is not a requirement. – fpmurphy Feb 17 '21 at 12:48
  • Can you demonstrate a case where quoting is actually necessary? No matter how I try, I can assign any value I want without quoting if I already managed to get it into `$1`. – tripleee Feb 17 '21 at 12:52

0 Answers0