Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7709245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:48:53+00:00 2026-06-01T00:48:53+00:00

I am studying the book Beginning Linux Programming 4th ed and chapter 2 is

  • 0

I am studying the book “Beginning Linux Programming 4th ed” and chapter 2 is about shell programming. I was impressed by the example on page 53, and tried to develop a script to display more on that. Here is my code:

enter code here
#!/bin/bash
var1=10
var2=20
var3=30
var4=40
for i in 1 2 3 4 # This works as intended!
do
    x=var$i
    y=$(($x))
    echo $x = $y   # But we can avoid declaring extra parameters x and y, see next line
    printf "  %s \n" "var$i = $(($x))"
done
for j in 1 2 3 4  #This has problems!
do
    psword=PS$j
    #eval psval='$'PS$i   # Produces the  same output as the next line
    eval psval='$'$psword
    echo '$'$psword = $psval 
    #echo "\$$psword = $psval"    #The same as previous line
    #echo  $(eval '$'PS${i})   #Futile attempts
    #echo PS$i =  $(($PS${i}))
    #echo PS$i =  $(($PS{i}))
done

#I can not make it work as I want : the output I expect is
#PS1 = \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$
#PS2 = >
#PS3 = 
#PS4 = + 

How can I get the intended output? When I run it as it is I only get

PS1 = 
PS2 = 
PS3 = 
PS4 = +

What happened with PS1 and PS2 ?
Why do not I get the same value that I get with

echo $PS1
echo $PS2
echo $PS3
echo $PS4

because that was what I am trying to get.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:48:54+00:00Added an answer on June 1, 2026 at 12:48 am

    Shell running a script is always non interactive shell. You may force to run the script in interactive mode using ‘-i’ option:

    Try to change:

    #!/bin/bash
    

    to:

    #!/bin/bash -i
    

    see INVOCATION section in ‘man bash’ (bash.bashrc is where your PS1 is defined):

           When an interactive shell  that  is  not  a  login  shell  is  started,  bash  reads  and  executes  commands  from
       /etc/bash.bashrc  and  ~/.bashrc,  if  these  files  exist.  This may be inhibited by using the --norc option.  The
       --rcfile file option will force bash to read and  execute  commands  from  file  instead  of  /etc/bash.bashrc  and
       ~/.bashrc.
    
       When  bash  is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in
       the environment, expands its value if it appears there, and uses the expanded value as the name of a file  to  read
       and execute.  Bash behaves as if the following command were executed:
              if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
       but the value of the PATH variable is not used to search for the file name.
    

    you can also read: http://tldp.org/LDP/abs/html/intandnonint.html

    simple test:

    $ cat > test.sh
    echo "PS1: $PS1"
    $ ./test.sh 
    PS1: 
    
    $ cat > test.sh
    #!/bin/bash -i            
    echo "PS1: $PS1"
    $ ./test.sh 
    PS1: ${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\] 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm studying this book (Addison Wesley Windows System Programming 4th Edition) and I think
I was recently studying the book named Advanced Linux Programming and I ran into
Currently I've finished studying the book name Beginning JavaScript 3rd Edition, so I just
I am studying the Windows programming by using the book Programming.Windows.5th.Edition(Charles Petzold). When I
I was studying the Kernel Architecture and its programming to get the idea about
I am studying x86 assembly language via the book called Programming from the Ground
I'm studying for SCJP, and my book (Sierra's) says about the Formatter class that
I'm studying Enterprise Patterns and I'm reading this book . I just read chapter
I'm currently studying the Inheritance chapter of a C# book. I'm unable to understand
I was going through a book studying Linked list and saw these lines if(

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.