-1

I noticed a solution on Codewars which had the following syntax:

#!/bin/bash
seven () {
    bc <<< "
    scale=0
    counter=0
    m=$1
    while( m > 99 ) {
        counter = counter + 1
        x = m / 10
        y = m % 10
        m = x - 2 * y
    }
    print m, \", \", counter
    "
}
seven "$1"

My question is regarding the variables used(m,x,counter). How is it that bash allows to use variables without using $variable_name?

Are there special cases (such as wraping code with double-quotes) that allow for this?

tankris
  • 1
  • 5
  • 2
    Those are `bc` variables, not `bash` variables. – oguz ismail Jul 22 '20 at 16:57
  • Sorry. I do not understand. Isn't bc just a tool that calculates big arithmetic expressions. How is it that running the script treats the variables any differently than if written outside the string passed to bc. Thanks in advance. – tankris Jul 22 '20 at 17:15
  • @tankris: Read `man bc` to understand how it can work with variables. – choroba Jul 22 '20 at 17:19
  • @choroba MAN! This is what I needed. Thank you so much. I was under the assumption that inside strings, bash programs had a different syntax. Thank you very much. – tankris Jul 22 '20 at 17:28

2 Answers2

2

These are not bash variables but bc variables.

The <<< operator introduces a "Here String" (see man bash), the following word undergoes expansions except for pathname expansion and word splitting and is sent to the standard input of the command, bc in this case.

You can include a program for any other interpreter this way, i.e.

python3 <<< 'x="Hello world!"
print(x)'

or

dc <<< '
100 3 /
p'

Also note that bash uses $x or ${x}, not $(x) (that would run the command x and return its output). $(x) for the variable x is used in Makefiles, though.

choroba
  • 231,213
  • 25
  • 204
  • 289
  • What I'd like to know is, are x,m, etc bash variables? If I'm not wrong, once declared, bash variables need to accessed using $variable_name. However, this program clearly doesn't do so (except in the case of $1 which I understand is the 1st argument). – tankris Jul 22 '20 at 17:07
  • The whole thing in the double quotes is a `bc` program. For bash, it's just a string. – choroba Jul 22 '20 at 17:14
  • Thanks for your prompt reply. I'm getting a little confused. What then is the language used inside the string? – tankris Jul 22 '20 at 17:19
  • As already mentioned, the string is sent to the standard input of the command on the left hand side of the `<<<`. If it happens to be a language interpreter (which `bc`, `python`, and `dc` are, among others), it interprets the string as a program and runs it. – choroba Jul 22 '20 at 17:21
1

There's virtually no bash code in that answer. It's a shell function that does nothing but run bc. Everything in the here string is a bc script, which has nothing to do with bash.

As far as bash is concerned, the here string doesn't contain any variables or any discernible structure: it's just opaque text that it will feed to bc's standard input.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Forgive me for my ignorance but if I am not wrong, this does fall under bash since it starts with !/bin/bash, right? – tankris Jul 22 '20 at 17:12
  • @tankris Your question is bash related since you were asking about bash's role in the interpretation of logic of this script. Given what you know now, if you were trying to fix a mathematical bug in the bc script, you can imagine it being purely a `bc` question and not a bash question (unless the problem is invoking the script correctly, rather than writing the script in the first place) – that other guy Jul 22 '20 at 18:03
  • @thatotherguy Thank you. I shall remove the bash tag. Kindly let me know if there is anything else I could do to improve the clarity in the question/description to help someone else with a similar issue. – tankris Jul 22 '20 at 18:44
  • 1
    You don't need to remove the tag; I was just trying to point out that `bash` isn't involved in the interpretation of the string. – chepner Jul 22 '20 at 18:45