pilcrow's answer provides an elegant solution; this is an explanation of why the OP's approach didn't work.
The main problem with the OP's approach was the attempt to assign to positional parameter $1
with $1=...
, which won't work.
The LHS is expanded by the shell to the value of $1
, and the result is interpreted as the name of the variable to assign to - clearly, not the intent.
The only way to assign to $1
in bash is via the set
builtin.
The caveat is that set
invariably sets all positional parameters, so you have to include the other ones as well, if any.
set -- "${1:-/dev/stdin}" "${@:2}" # "${@:2}" expands to all remaining parameters
(If you expect only at most 1 argument, set -- "${1:-/dev/stdin}"
will do.)
The above also corrects a secondary problem with the OP's approach: the attempt to store the contents rather than the filename of stdin in $1
, since <
is used.
${1:-/dev/stdin}
is an application of bash parameter expansion that says: return the value of $1
, unless $1
is undefined (no argument was passed) or its value is the empty string (""
or ''
was passed). The variation ${1-/dev/stdin}
(no :
) would only return /dev/stdin
if $1
is undefined (if it contains any value, even the empty string, it would be returned).
If we put it all together:
# Default to filename '/dev/stdin' (stdin), if none was specified.
set -- "${1:-/dev/stdin}" "${@:2}"
while read -r line; do
... # find the longest line
done < "$1"
But, of course, the much simpler approach would be to use ${1:-/dev/stdin}
as the filename directly:
while read -r line; do
... # find the longest line
done < "${1:-/dev/stdin}"
or, via an intermediate variable:
filename=${1:-/dev/stdin}
while read -r line; do
... # find the longest line
done < "$filename"