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 7652533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:47:43+00:00 2026-05-31T11:47:43+00:00

Lets say I have a shell / bash script named test.sh with: #!/bin/bash TESTVARIABLE=hellohelloheloo

  • 0

Lets say I have a shell / bash script named test.sh with:

#!/bin/bash

TESTVARIABLE=hellohelloheloo
./test2.sh

My test2.sh looks like this:

#!/bin/bash

echo ${TESTVARIABLE}

This does not work. I do not want to pass all variables as parameters since imho this is overkill.

Is there a different way?

  • 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-05-31T11:47:45+00:00Added an answer on May 31, 2026 at 11:47 am

    You have basically two options:

    1. Make the variable an environment variable (export TESTVARIABLE) before executing the 2nd script.
    2. Source the 2nd script, i.e. . test2.sh and it will run in the same shell. This would let you share more complex variables like arrays easily, but also means that the other script could modify variables in the source shell.

    UPDATE:

    To use export to set an environment variable, you can either use an existing variable:

    A=10
    # ...
    export A
    

    This ought to work in both bash and sh. bash also allows it to be combined like so:

    export A=10
    

    This also works in my sh (which happens to be bash, you can use echo $SHELL to check). But I don’t believe that that’s guaranteed to work in all sh, so best to play it safe and separate them.

    Any variable you export in this way will be visible in scripts you execute, for example:

    a.sh:

    #!/bin/sh
    
    MESSAGE="hello"
    export MESSAGE
    ./b.sh
    

    b.sh:

    #!/bin/sh
    
    echo "The message is: $MESSAGE"
    

    Then:

    $ ./a.sh
    The message is: hello
    

    The fact that these are both shell scripts is also just incidental. Environment variables can be passed to any process you execute, for example if we used python instead it might look like:

    a.sh:

    #!/bin/sh
    
    MESSAGE="hello"
    export MESSAGE
    ./b.py
    

    b.py:

    #!/usr/bin/python
    
    import os
    
    print 'The message is:', os.environ['MESSAGE']
    

    Sourcing:

    Instead we could source like this:

    a.sh:

    #!/bin/sh
    
    MESSAGE="hello"
    
    . ./b.sh
    

    b.sh:

    #!/bin/sh
    
    echo "The message is: $MESSAGE"
    

    Then:

    $ ./a.sh
    The message is: hello
    

    This more or less “imports” the contents of b.sh directly and executes it in the same shell. Notice that we didn’t have to export the variable to access it. This implicitly shares all the variables you have, as well as allows the other script to add/delete/modify variables in the shell. Of course, in this model both your scripts should be the same language (sh or bash). To give an example how we could pass messages back and forth:

    a.sh:

    #!/bin/sh
    
    MESSAGE="hello"
    
    . ./b.sh
    
    echo "[A] The message is: $MESSAGE"
    

    b.sh:

    #!/bin/sh
    
    echo "[B] The message is: $MESSAGE"
    
    MESSAGE="goodbye"
    

    Then:

    $ ./a.sh
    [B] The message is: hello
    [A] The message is: goodbye
    

    This works equally well in bash. It also makes it easy to share more complex data which you could not express as an environment variable (at least without some heavy lifting on your part), like arrays or associative arrays.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have an array like this: string [] Filelist = ... I
Lets say i have this in a shell chdir * && whoami.exe >> $$$
Say I have a file at the URL http://mywebsite.example/myscript.txt that contains a script: #!/bin/bash
Let's say I have a script like the following: useless.sh echo This Is Error
lets say I have my owndefined datatype like this: public class InformationType { private
Lets say have this immutable record type: public class Record { public Record(int x,
Lets say we have something like: <div class=row> <div class=box> <a class=more href=#more/> </div>
I have a list like this (let's say it is memorized in summ.txt): s1
Lets say I have a Dictionary object: Dictionary myDictionary<int, SomeObject> = new Dictionary<string, SomeObject>();
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends

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.