I have a function myFunction() that shall take an integer variable from outside, print it and then increment it. This works: myFunction: myFunction() {     local var=&quot;${1}&quot;     eval &quot;printf '%s\n' \$$var&quot;     eval &quot;$var=$(($var+1))&quot; }  But, I don't want to use eval in this as a user of this function might enter a wrong variable name that might then be executed. I would like to use something like printf -v as it would make the usage safer. How can I do this?
121076
I want to ask if it is possible to pass arguments to a script function by reference:  i.e. to do something that would look like this in C++:  void boo(int &amp;myint) { myint = 5; }  int main() {     int t = 4;     printf("%d\n", t); // t-&gt;4     boo(t);     printf("%d\n", t); // t-&gt;5 }   So then in BASH I want to do something like:  function boo ()  {      var1=$1       # now var1 is global to the script but using it outside                   # this function makes me lose encapsulation      local var2=$1 # so i should use a local variable ... but how to pass it back?      var2='new'    # only changes the local copy      #$1='new'     this is wrong of course ...     # ${!1}='new' # can i somehow use indirect reference? }             # call boo SOME_VAR='old' echo $SOME_VAR # -&gt; old boo "$SOME_VAR" echo $SOME_VAR # -&gt; new   Any thoughts would be appreciated.
27528
I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing.    Is there a function that can do this easily?  For example:  $string1 = "I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing."; $string2 = 100charfunction($string1); print $string2   To get:  I am looking for a way to pull the first 100 characters from a string vari
22243
I am having difficulty getting a for loop to take user input in the form of an argument to a function I created. #!/bin/bash number1=&quot;$1&quot;    #Assume user input 5 loops=&quot;$2&quot;      #Assume user input 5 if [[ ${number1} &amp;&amp; ${loops} =~ ^-?[0.00-9.99]+$ ]];then    for number2 in {1..${loops}}    do        Product=$(echo &quot;${number1} * ${number2}&quot; | bc -l)        echo &quot;${number1} * ${number2} = ${Product}&quot;        if test ${number2} -eq 7;then            { echo &quot;----------------------&quot;; echo &quot;${number2} is the best number.&quot;; echo &quot;----------------------&quot;; continue; }        elif test ${number2} -eq 11;then            { echo &quot;------------------------------&quot;; echo &quot;${number2} is pretty awesome as well.&quot;; echo &quot;------------------------------&quot;; continue; }            fi     done else    { echo &quot;Please try again with a valid numerical entry.&quot;; exit 1; } fi  This script does not run when the user inputs an integer for the variable &quot;loops&quot;, and any number for &quot;number1&quot;. number1 = 5 in this case. It returns the error:  (standard_in) 1: syntax error 5 * {1..5} = /home/$USER/For_Loop_Multiplication.sh: line 9: test: {1..5}: integer expression expected /home/$USER/For_Loop_Multiplication.sh: line 11: test: {1..5}: integer expression expected  If I instead directly input an end bound on the number of loops directly into the script like the following, and number1 = 5, it runs fine: #!/bin/bash number1=&quot;$1&quot;     #Assume user input 5 if [[ ${number1} =~ ^-?[0.00-9.99]+$ ]];then    for number2 in {1..5}    do        Product=$(echo &quot;${number1} * ${number2}&quot; | bc -l)        echo &quot;${number1} * ${number2} = ${Product}&quot;        if test ${number2} -eq 7;then            { echo &quot;----------------------&quot;; echo &quot;${number2} is the best number.&quot;; echo &quot;----------------------&quot;; continue; }        elif test ${number2} -eq 11;then            { echo &quot;------------------------------&quot;; echo &quot;${number2} is pretty awesome as well.&quot;; echo &quot;------------------------------&quot;; continue; }            fi     done else    { echo &quot;Please try again with a valid numerical entry.&quot;; exit 1; } fi  It returns the result:  5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25  Essentially what I think is happening is that the script seems to be placing the entire range as the value number2, which is defined as the iterator in the for loop. Since the input for the variable &quot;loops&quot; is an integer, there should be no difference between asking the user for a range and placing it in the loop, or simply setting it as a default when I write the script. However, there apparently is a difference... Any insight into solving this problem is appreciated. Thanks.
15222
After reading ilkkachu's answer to  I learned on the existence of the declare (with argument -n) shell built in.  help declare brings:     Set variable values and attributes.      Declare variables and give them attributes. If no NAMEs are given,   display the attributes and values of all variables.      -n ... make NAME a reference to the variable named by its value   I ask for a general explanation with an example regarding declare because I don't understand the man. I know what is a variable and expanding it but I still miss the man on declare (variable attribute?).  Maybe you'd like to explain this based on the code by ilkkachu in the answer:  #!/bin/bash function read_and_verify  {     read -p "Please enter value for '$1': " tmp1     read -p "Please repeat the value to verify: " tmp2     if [ "$tmp1" != "$tmp2" ]; then         echo "Values unmatched. Please try again."; return 2     else         declare -n ref="$1"         ref=$tmp1     fi }
4791
Say i have this PHP code:  $FooBar = "a string";   i then need a function like this:  print_var_name($FooBar);   which prints:  FooBar   Any Ideas how to achieve this? Is this even possible in PHP?
32795
Why is it that scanf() needs the l in "%lf" when reading a double, when printf() can use "%f" regardless of whether its argument is a double or a float?  Example code:  double d; scanf("%lf", &amp;d); printf("%f", d);
92770
Right now, I have code that looks something like this:  Private Sub ShowReport(ByVal reportName As String)     Select Case reportName         Case "Security"             Me.ShowSecurityReport()         Case "Configuration"             Me.ShowConfigurationReport()         Case "RoleUsers"             Me.ShowRoleUsersReport()         Case Else             pnlMessage.Visible = True             litMessage.Text = "The report name """ + reportName + """ is invalid."     End Select End Sub   Is there any way to create code that would use my method naming conventions to simplify things?  Here's some pseudocode that describes what I'm looking for:  Private Sub ShowReport(ByVal reportName As String)     Try         Call("Show" + reportName + "Report")     Catch ex As Exception         'method not found     End Try End Sub
19595
I quite often see JavaScript with variables that start with a dollar sign. When/why would you choose to prefix a variable in this way?  (I'm not asking about $('p.foo') syntax that you see in jQuery and others, but normal variables like $name and $order)
115823
Many times when I am watching others code I just want to find where and how a variable is defined. Normally what I do now is look for the type of the variable until I find the definition, that is very time consuming. And I guess that there are some tools that can help me in this rutinary situation. Any suggestion in some tools or commands to help me in this task?.  I know that using a GUI and creating a project this is done automatically I am talking of a way to do this without a GUI. I am working with only text mode. I am running under Linux and I am using C/C++, but suggestions for other languages are welcome.  Thanks a lot.  A possible solution  Michel in one of his comments propose a simple an effective solution define again the variable, in that case in compilation time, the compiler will inform where is the previous definiton. Of course to apply this solution we need to think previously in the locality of the variable.
81621
If I write the following class:  public class Example {        int j;       int k;        public Example(int j, int k) {            j = j;            k = k;       }        public static void main(String[] args) {            Example exm = new Example(1,2);            System.out.println(exm.j);            System.out.println(exm.k);       }  }   The program compiles, but when I run the program, the main method will print out two 0s. I know that in order to say that I want to initialize the instance variables in the constructor I have to write:  this.j = j; this.k = k;   But if I don't write it, then which variable is evaluated (or considered) in the constructor (on the left and on the write hand side of the expressions)? Is is the argument or the instance variable? Does it make a difference?  Are there other cases where the use of this is obligatory?
3114
I'd like to do it in Python. What I'd like to do in this example in C: #include &lt;stdio.h&gt;  int main() {     int i;     for (i=0; i&lt;10; i++) printf(&quot;.&quot;);     return 0; }  Output: ..........  In Python: &gt;&gt;&gt; for i in range(10): print('.') . . . . . . . . . . &gt;&gt;&gt; print('.', '.', '.', '.', '.', '.', '.', '.', '.', '.') . . . . . . . . . .  In Python print will add a \n or space, how can I avoid that? Now, it's just an example, don't tell me I can first build a string then print it. I'd like to know how to &quot;append&quot; strings to stdout.
100903
Most languages have naming conventions for variables, the most common style I see in shell scripts is MY_VARIABLE=foo.  Is this the convention or is it only for global variables?  What about variables local to the script?
45658
I want to use printf to print a variable. It might be possible that this variable contains a % percent sign.  Minimal example:  $ TEST="contains % percent" $ echo "${TEST}" contains % percent $ printf "${TEST}\n" bash: printf: `p': invalid format character contains $   (echo provides the desired output.)
102637
I am reading the book 'Practical Common Lisp' by Peter Seibel. In Chapter 6, &quot;Variables&quot; sections &quot;Lexical Variables and Closures&quot; and &quot;Dynamic, a.k.a. Special, Variables&quot;.  My problem is that the examples in both sections show how (let ...) can shadow global variables and doesn't really tell the difference between the Dynamic and Lexical vars. I understand how closures work but I don't really get whats so special about let in this example: (defvar *x* 10)  (defun foo ()   (format t &quot;Before assignment~18tX: ~d~%&quot; *x*)   (setf *x* (+ 1 *x*))   (format t &quot;After assignment~18tX: ~d~%&quot; *x*))   (defun bar ()   (foo)   (let ((*x* 20)) (foo))   (foo))   CL-USER&gt; (foo) Before assignment X: 10 After assignment  X: 11 NIL   CL-USER&gt; (bar) Before assignment X: 11 After assignment  X: 12 Before assignment X: 20 After assignment  X: 21 Before assignment X: 12 After assignment  X: 13 NIL  I feel like there is nothing special is going on here. The outer foo in bar increments the global x, and foo surrounded by let in bar increments the shadowed x. What's the big deal? I don't see how is this supposed to explain the difference between lexical and dynamic variables. Yet the book continues like this:  So how does this work? How does LET know that when it binds x it's supposed to create a dynamic binding rather than a normal lexical binding? It knows because the name has been declared special.12 The name of every variable defined with DEFVAR and DEFPARAMETER is automatically declared globally special.  What would happen if let would bind x using &quot;normal lexical binding&quot;? All in all, what are the differences between dynamic and lexical binding and how is this example special regarding dynamic binding?
78348
You can do this in Python, but is it possible in PHP?  &gt;&gt;&gt; def a(): print 1 ...  &gt;&gt;&gt; def a(): print 2 ...  &gt;&gt;&gt; a() 2   e.g.:  &lt;? function var_dump() {} ?&gt; Fatal error: Cannot redeclare var_dump() in /tmp/- on line 1
42864
I mean other than using it when required for functions, classes, if, while, switch, try-catch.  I didn't know that it could be done like .  In the above link, Eli mentioned that "They use it to fold up their code in logical sections that don't fall into a function, class, loop, etc. that would usually be folded up."  What other uses are there besides those mentioned?   Is it a good idea to use curly braces to limit the scope of your variables and expand the scope only if required (working on a "need-to-access" basis)? Or is it actually silly?   How about using scopes just so that you can use the same variable names in different scopes but in the same bigger scope? Or is it a better practise to reuse the same variable (if you want to use the same variable name) and save on deallocating and allocating (I think some compilers can optimise on this?)? Or is it better to use different variable names altogether?
105039
The following code works:  \def \robble {robble} \def \robbles {\robble\robble}   But the following but doesn't:  \def \robbles {robble} \def \robbles {\robbles robble}   The overall goal is to get robbles to be robblerobble. I need something like the second example, where I am appending on to the current value of a variable. The way it is written right now causes a stack overflow. Is there a way to append to variables in LaTeX?