I think the problems you experienced can in general be attributed to the handling of special characters and quoting in your shell script.
As a leading remark: you didn't mention how your child script gets access to the functions in the main script, but I assume you do something like
~$ source main_script.sh
~$ export now=$( date '+%Y%m%d' )
~$ Function_1 'abc_${now}'
If I do so, my shell complains about several syntax errors, and the script wouldn't work. I assume you also use the bash
since you used that tag, and I admit that I am surprised you did not encounter these errors. You may still want to consider using a tool like shellcheck
, available online or as software package in many Linux distributions, to verify your shell-scripts - it certainly helps me finding typos and the like.
Now to the issues you encountered.
You state that the first echo
output never occurs. I think this is related to the construction of your regular expression, and the use of special characters:
- In the test statement
[[ "$File_name" =~ ^[[:alnum:]][[$_."#&']] ]]
, you use a character list [$_."#&']
with characters that are special to the shell: The $_
for example, will likely be interpreted as the last shell argument, and the double-quotes "
will be interpreted by the shell as the beginning of a literal string, which ends in the next line with the first "
of your echo
command. This should throw a syntax error, beacuse the shell tries to interpret your output message as shell commends; it definitely does so when I try it. You should make sure to escape such characters to inhibit unwanted interpretation by the shell, as in \$_\"\#\&\'
.
- Also, you use double square brackets
[[ ... ]]
for a character list. However, this is syntactically undefined. What is defined is a character list [ ... ]
consisting of only one character class, e.g. [:alnum:]
. This kind of construct would indeed be written as [[:alnum:]]
, as you already did in the first part of your regular expression. You should therefore omit the "outer" pair of square brackets.
- You have started your regular expression with the "start anchor"
^
. I assume therefore that you want to allow filenames that start with a regular letter or number, but then may contain all special characters you mentioned. However, your regular expression allows only for filenames starting with one alphanumeric character and then requiring one of the special characters as second character. If you want to ensure that any number of these are allowed in addition to alphanumeric characters, you would instead write something like ^[[:alnum:]][[:alnum:]\$_.\"\#\&\']*$
. The second part is then a character list [ ... ]
containing the character class [:alnum:]
and in addition the special characters $
, _
, .
, "
, #
, &
and '
, zero or more times (*
) until the end of the string ($
). This last anchor character ensures that the entire filename consists only of the characters you allowed.
Your next point concerns that the ${now}
variable is not resolved (i.e. "expanded" in the shell terminology).
As was already noted in several comments, parameter expansion does not happen inside single quotes ' ... '
. If you use double quotes " ... "
instead, the variable expansion will work.
You seem to be certain that you need single quotes at this point. It that is really so, there is little you can do; in that case, you may need to further explain why you need the single quotes (i.e. what you want to achieve, or whether further processing of the expression 'abc_${now}'
including the quotes is desired later in the process).
Function_1 'abc_${now}'
will prevent variable expansion. – muru Feb 04 '20 at 08:40"$File_name" =~ ^[[:alnum:]][[$_."#&']]
is accepting multipleunderscore _
in file name. For issue-2 is there any way to resolve the variable within single quotes. – user78873 Feb 04 '20 at 12:41" ... "
should do what you want. Here is a good reference on quoting in bash. – AdminBee Feb 04 '20 at 12:55'....'
– user78873 Feb 06 '20 at 03:20