which quotes and brackets to use within a ()
Hi,
I have a variable - lastfile which I need to set to the last modified tar file in a directory, named $PROJECTNAME. The script below works without $PROJECTNAME just fine, but how can I include it as the filename within the ()? lastfile=$(find . -type f -name '${PROJECTNAME}*.tar.gz' -printf '%p\n' | sort -nr | head -n 1 | sed 's/^..//') |
Quote:
lastfile=$(find . -type f -name "$PROJECTNAME*.tar.gz" -printf '%p\n' | sort -nr | head -n 1 | sed 's/^..//') |
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.
"$(..)" command substitution acts as a sub-shell. That means it's its own execution environment. Everything inside runs exactly like a command written on the main command line. As for quoting, it's very important to understand how the shell processes them and arguments. See here: http://mywiki.wooledge.org/Arguments http://mywiki.wooledge.org/WordSplitting http://mywiki.wooledge.org/Quotes By the way, head isn't needed if sed is also being used: Code:
... | sort -nr | sed -n '1s/^..//p') Code:
files=( "$PROJECTNAME"*.tar.gz ) How can I find the latest (newest, earliest, oldest) file in a directory? http://mywiki.wooledge.org/BashFAQ/003 How can I get the newest (or oldest) file from a directory? http://mywiki.wooledge.org/BashFAQ/099 |
All times are GMT -5. The time now is 01:56 PM. |