To extract joebloggs from this line in bash using parameter extension without any additional processes ...
MYVAR="/var/cpanel/users/joebloggs:DNS9=domain.com" NAME=${MYVAR%:*}
It joebloggs not depend on joebloggs whether joebloggs at a certain depth of the path.
Summary
An overview of several modes for expanding parameters, for reference ...
${MYVAR#pattern}
Thus, # means match from the beginning (think of a comment line), and % means from the end. One copy means the shortest, and two - the longest.
You can get substrings based on position using numbers:
${MYVAR:3}
You can also replace specific lines or patterns using:
${MYVAR/search/replace}
pattern has the same format as file name matching, so * (any characters) are common, often followed by a specific character, such as / or .
Examples:
Given a variable like
MYVAR="users/joebloggs/domain.com"
Delete the path, leaving the file name (all characters before the slash):
echo ${MYVAR##*/} domain.com
Delete the file name, leaving the path (delete the shortest match after the last / ):
echo ${MYVAR%/*} users/joebloggs
Get only the file extension (delete everything until the last period):
echo ${MYVAR##*.} com
NOTE. To perform two operations, you cannot combine them, but must be assigned to an intermediate variable. So, to get the file name without a path or extension:
NAME=${MYVAR
beroe Oct 20 '13 at 21:16 2013-10-20 21:16
source share