i am using linux and the below is script i am trying. I am trying to get the latest modified file in a folder:
#!/bin/bash
shopt -s extglob
name=$(echo *(om[1]))
echo $name
i am expecting filename instead *(om[1])
is echoed. As such the script does not give any error due to using shopt.
i try the command on commandline it gives the filename as output.
% cd other
% echo *(om[1])
mumbai123.txt
why echo *(om[1])
is working on commandline
but not in bash script
.
You are confusing zsh
glob qualifiers with extended patterns. In zsh
(your interactive shell), the expression *(om[1])
applies the o
and m
qualifiers to the pattern *
. In bash
, the extended pattern *(om[1])
matches zero or more occurrences of the pattern om[1]
(which matches the literal string "om1"). Since there is no file matching that pattern, it is passed literally to echo
, which outputs it to be captured as the value of name
.