• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Why Use the Magic Number in Qshell Scripts?

    March 17, 2004 Hey, Ted

    I have not worked with Unix, so Qshell is a new adventure for me. I am curious about the “magic number,” the #! comment that shell scripts typically begin with. It appears to me that Qshell doesn’t care whether you include the comment. Is that true? If so, is there a reason why I should include the magic number?

    –Jim

    It’s true. Take a look at the process Qshell goes through to execute a script.

    When you type a script name and press Enter, Qshell starts a new job and asks the system to run the script. The system reads the first line of the script to see whether it contains the magic number. If the system finds the magic number, it loads the executable file named in the magic number, passing the name of the script to the executable file in the first positional parameter. Suppose the first line of script file Example, located in directory /home, contains the following magic number.

    #! /usr/bin/qsh
    

    The system runs the following command:

    /usr/bin/qsh /home/Example
    

    The result is that the system loads Qshell and runs the script, which is what you wanted.

    Now suppose that the magic number is omitted. The system reads the first line of the script but does not find a magic number. In this case, the system invokes Qshell anyway, passing it the script file name in the first parameter. Qshell attempts to run the script.

    This brings us back to your question. If Qshell ends up running the script anyway, why should you include the magic number? In your situation, it may not matter. I can think of a couple of reasons why it’s a good idea to use the magic number.

    First, the magic number leaves no doubt that the script was written for Qshell. Scripts written for PASE shells can run a Qshell script simply by executing the Qshell script file name.

    Second, whatever your feelings for Unix and Linux are, the fact is that use of these operating systems is growing in iSeries shops. Chances are good you’ll have to work with either or both of them, and you will need to include the magic number in scripts written for those environments. Why not adopt the practice now?

    –Ted

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    ARCAD Software

    Embrace VS Code for IBM i Development

    The IBM i development landscape is evolving with modern tools that enhance efficiency and collaboration. Ready to make the move to VS Code for IBM i?

    Join us for this webinar where we’ll showcase how VS Code can serve as a powerful editor for native IBM i code and explore the essential extensions that make it possible.

    In this session, you’ll discover:

    • How ARCAD’s integration with VS Code provides deep metadata insights, allowing developers to assess the impact of their changes upfront.
    • The role of Git in enabling seamless collaboration between developers using tools like SEU, RDi, and VS Code.
    • Powerful extensions for code quality, security, impact analysis, smart build, and automated RPG conversion to Free Form.
    • How non-IBM i developers can now contribute to IBM i projects without prior knowledge of its specifics, while ensuring full control over their changes.

    The future of IBM i development is here. Let ARCAD be your guide!

    Watch Now

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    DRV Technologies Expands Output Options with SpoolFlex 3.0 Power5 Debuts March 31, OS/400 V5R3 Coming in April

    One thought on “Why Use the Magic Number in Qshell Scripts?”

    • Glenn Gundermann says:
      April 26, 2022 at 3:39 pm

      Hi Ted,

      I have inherited a CL program with the following command:
      QSH CMD(‘/QOpenSys/bin/ksh93 /some_folder/some_script’)

      On the first line of the script is the following:
      #!/QOpenSys/usr/bin/ksh93

      Notice the path to ksh93 is different.

      I presume the CL program will start ksh in /QOpenSys/bin.

      What will happen next? Does the script execute ksh93 in QOpenSys/usr/bin and run a blank script and then return? Will it transfer control to ksh93 in QOpenSys/usr/bin and run the remainder of the script?

      Does #! affect the path?

      I’m not using a .profile file in my home directory. I’ve created a file by the name of profile in the /etc folder that looks like the following:
      export PATH=/QOpenSys/pkgs/bin:$PATH
      If I start a session using QSH and echo $PATH, it looks like:
      /QOpenSys/pkgs/bin:/usr/bin:.:/QOpenSys/usr/bin

      If I insert echo $PATH inside my script, I get a different answer and I don’t know why.
      /QOpenSys/usr/bin:/usr/ccs/bin:/QOpenSys/usr/bin/X11:/usr/sbin:.:/usr/bin

      As you can see, there is no mention of pkgs and it has other folders in the path.

      Thank you.

      Reply

    Leave a Reply Cancel reply

Volume 4, Number 8 -- March 17, 2004
THIS ISSUE
SPONSORED BY:

Guild Companies
COMMON
iTera
WorksRight Sofware
Damon Technologies

Table of Contents

  • V5R3 CL Programming Enhancements, Part 1
  • Using ‘New’ SQL Data Types in RPG
  • Why Use the Magic Number in Qshell Scripts?
  • Admin Alert: The Absolute Coolness of the QNTC File System
  • OS/400 Alert: Major Outlook 2002 Vulnerability Discovered

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Liam Allan Shares What’s Coming Next With Code For IBM i
  • From Stable To Scalable: Visual LANSA 16 Powers IBM i Growth – Launching July 8
  • VS Code Will Be The Heart Of The Modern IBM i Platform
  • The AS/400: A 37-Year-Old Dog That Loves To Learn New Tricks
  • IBM i PTF Guide, Volume 27, Number 25
  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle