basherror-handlingreturn-valueexit-code

How do I set $? or the return code in Bash?


I want to set a return value once so it goes into the while loop:

#!/bin/bash
while [ $? -eq 1 ]
do
#do something until it returns 0    
done

In order to get this working I need to set $? = 1 at the beginning, but that doesn't work.


Solution

  • #!/bin/bash
    
    RC=1
    
    while [ $RC -eq 1 ]
    do
    
      #do something until it returns 0    
    
      RC=$?
    done