Bash tilde *substring* expansion - undocumented feature?

kojiro

I was surprised that this expansion:

$ echo "${foo:~abc}"

yielded the empty string when foo was unset. I expected that it would parse like this:

$ echo "${foo:(~abc)}"

and yield the string "~abc". But instead, I found that if I did define

$ foo='abcdefg'
$ echo "${foo:~abc}"
g

In fact, it's taking "abc" in arithmetic context and doing. "${foo:~0}". Likewise

$ foo='abcdefg'
$ echo "${foo:~3}"
defg

It gets you the last n+1 characters of the expansion. I looked in the "Parameter Expansion" section of the manpage. I see no mention of tildes there. Bash Hackers Wiki only mentions tildes as (also undocumented) case modifiers.

This behavior goes back to at least 3.2.57.

Am I just missing where this form of substring expansion is documented, or is it not documented at all?

chepner

It's not undocumented (you may have been confusing ${foo:~abc} with ${foo-~abc}).

${parameter:offset}
${parameter:offset:length}
     Substring Expansion.  Expands to up to length characters of  the
     value  of  parameter starting at the character specified by off-
     set.  [...]  If length is omitted, expands to the  substring  of  the
     value of parameter starting at the character specified by offset
     and extending to the end of the value.  length  and  offset  are
     arithmetic expressions (see ARITHMETIC EVALUATION below).

Here, ~abc is the offset field of the expansion, and ~ is the bitwise negation operator in the arithmetic expression. An undefined parameter evaluates to 0 in an arithmetic expression, and ~0 == -1.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Inconsistent behavior when replacing substring with tilde "~" in a BASH parameter expansion

From Dev

Is `~#` a tilde expansion of bash?

From Dev

Bash substring expansion on array

From Dev

Bash substring expansion on array

From Dev

Bash compound substring expansion

From Dev

combining tilde expansion with variable substitution in bash

From Dev

customizing ZSH tilde expansion

From Dev

Tilde expansion without eval

From Dev

Expansion of tilde in zsh

From Dev

Google Realtime undocumented feature: 'toJson'?

From Dev

Google Realtime undocumented feature: 'toJson'?

From Dev

tilde expansion when evaluating $PATH

From Dev

The Not Equal Tilde in bash

From Dev

Difference between tilde expansion in RStudio and R

From Dev

How to avoid tilde ~ in Bash prompt?

From Dev

undocumented change of behaviour of 'local' builtin in bash 4.3?

From Dev

undocumented change of behaviour of 'local' builtin in bash 4.3?

From Dev

How do I proc out with tilde expansion AND $PATH searching in Haskell?

From Dev

Why isn't tilde expansion performed on the input to read?

From Dev

Why isn't tilde expansion performed on the input to read?

From Dev

Bash variable expansion in command

From Dev

Brace expansion with numbers in bash

From Dev

: and := inside a bash parameter expansion

From Dev

Bash for loop and glob expansion

From Dev

Inline expansion of arrays in bash

From Dev

Bash pairwise array expansion

From Dev

weird bash alias expansion

From Dev

BASH brace expansion algorithm

From Dev

Bash variable expansion