What does echo $((2#$1)) exactly does?
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle4
--
Chapters
00:00 What Does Echo $((2#$1)) Exactly Does?
00:27 Accepted Answer Score 76
01:04 Answer 2 Score 31
02:01 Answer 3 Score 26
02:53 Thank you
--
Full question
https://superuser.com/questions/1162555/...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#bash
#avk47
ACCEPTED ANSWER
Score 76
man bash
echo [-neE] [arg ...]
Output the args, separated by spaces, followed by a newline.
The return status is 0 unless a write error occurs. If -n is
specified, the trailing newline is suppressed. If the -e option
is given, interpretation of the following backslash-escaped
characters is enabled.
[...]
Arithmetic Expansion
Arithmetic expansion allows the evaluation of an arithmetic expression
and the substitution of the result. The format for arithmetic expan‐
sion is:
$((expression))
[...]
Constants with a leading 0 are interpreted as octal numbers. A leading
0x or 0X denotes hexadecimal. Otherwise, numbers take the form
[base#]n, where the optional base is a decimal number between 2 and 64
representing the arithmetic base, and n is a number in that base. If
base# is omitted, then base 10 is used. When specifying n, the digits
greater than 9 are represented by the lowercase letters, the uppercase
letters, @, and _, in that order. If base is less than or equal to 36,
lowercase and uppercase letters may be used interchangeably to repre‐
sent numbers between 10 and 35.
ANSWER 2
Score 31
From the Doc at: https://tiswww.case.edu/php/chet/bash/bashref.html#Shell-Arithmetic
Constants with a leading 0 are interpreted as octal numbers. A leading ‘0x’ or ‘0X’ denotes hexadecimal. Otherwise, numbers take the form [base#]n, where the optional base is a decimal number between 2 and 64 representing the arithmetic base, and n is a number in that base. If base# is omitted, then base 10 is used. When specifying n, the digits greater than 9 are represented by the lowercase letters, the uppercase letters, ‘@’, and ‘_’, in that order. If base is less than or equal to 36, lowercase and uppercase letters may be used interchangeably to represent numbers between 10 and 35.
So echo $((16#FF))
outputs 255
and echo $((2#0110))
outputs 6
ANSWER 3
Score 26
Ipor's answer is excellent but very slightly incomplete. The quoted part of the bash man page states that the [base#]n
syntax works only for constants, and 2#$1
is not a constant. You should be asking how this really works!
EXPANSION
Expansion is performed on the command line after it has been split into words. There are seven kinds of expansion performed: brace expansion, tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, word splitting, and pathname expansion.
The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion, arithmetic expansion, and command substitution (done in a left-to-right fashion); word splitting; and pathname expansion.
Basically Bash is doing variable substitution first, so that the $1
is first replaced with its value. Only then does it do arithmetic expansion, which sees only a proper constant.