Hello, I have been learning Linux shell commands for about 4 months now. I am trying to write a simple shell script, but I have encountered an issue I do not understand the cause of.
When I run my script I get an error.
[user@x dir]$ ./myscript.sh
cp: cannot stat `path/to/file/{1..2}.dat': No such file or directory
[user@x dir]$
However when I run it without variables.
#!/bin/bash
cp path/to/files/{1..10}.dat ./
Running script without variables.
[user@x dir]$ ./myscript.sh
It copies the ten *.dat files from the path/to/files to the directory of myscript.sh. How can I get the script working with variables? Thanks in advance for any help given.
Good to know. Bash was first with brace expansion, now ksh and zsh are ahead. Let's see when bash will catch up again.
BTW, Bash-4 has got a number + field alignment expansion:
(see above), as opposed to "file name globbing". So be careful and test for file existence (as done in other posts / methods above) to avoid running into errors.