Use of % in variables

Asked

Viewed 70 times

3

I can not understand what this symbol serves in a variable, I see its use in some points in codes in SQL, Shell Script, C. For what it serves?

For example a use of this symbol in Shel Script:

DATA=$(date +%H)

The variable DATA receives the (current date+ %H)

I don’t understand what the '%'

  • 2

    Depends on the language. It is usually used as the operator MOD, but this has no relation to variables. I’ve never seen this symbol in a variable in Java. You can give an example?

  • In general this is Mod which is the rest of the division, for example 8 % 3 = 2. This is math and not programming, without an example it is impossible to say "what he is doing".

1 answer

3


In Shell Script this is an indicator formatting, so it will take the result of date and will apply the format that the programmer establishes right after, in this case the %H indicates that you only want to catch the time. The symbol %, in this context, it is only to indicate which is the pattern you want, to have no ambiguity with normal letters.

Of course in other languages there are similar things. It is a character of escape indicated that the following is a formatting code and not a normal character to be displayed.

This is not used in variables but in literals string.

  • That was my question! Thank you very much!

  • 1

    @Legal Matheusrambo, now you can vote on everything on the site too.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.