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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:28:26+00:00 2026-05-14T04:28:26+00:00

Here is my script #!/usr/bin/env ruby if __FILE__ == $0 `cd ..` puts `ls`

  • 0

Here is my script

#!/usr/bin/env ruby
if __FILE__ == $0
    `cd ..` 
    puts `ls` 
end

which runs fine, but when it exits, I’m back where I started. How can I “export” the changes I’ve made to the environment?

  • 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-14T04:28:26+00:00Added an answer on May 14, 2026 at 4:28 am

    That is because the backtick operator is not intended for complicated
    scripting. It is useful for running a single command (and capturing
    its output). There is no shell behind it to store environment changes
    between its calls and after your Ruby script is terminated.

    On Linux systems each process has its own current directory path (it
    could be found in /proc/‹pid›/cwd). Changing directory in a process
    does not affect parent processes (the shell you run program from). If
    the cd built-in were a binary, it could change only its own current
    directory, not the one of the parent process, and that is why the cd
    command could be built-in only.


    An alternative implementation

    If a Ruby script must be executed from shell and must affect the
    shell’s current directory path, the following trick may be used.
    Instead of running commands from within Ruby, print those commands to
    the standard output, and then source it to the shell you are running
    the Ruby script from. The commands will not be executed by a separate
    process of a shell, so all cds will take effect in the current
    shell instance.

    So, instead of

    ruby run_commands.rb
    

    write in your shell-script something like that:

    source <(ruby prepare_and_print_commands.rb)
    

    The Shell class

    But there is a convenient tool for command line scripting in Ruby: the
    Shell class! It has predefined shortenings for frequently used
    commands (such as cd, pwd, cat, echo, etc.) and allows to
    define your own (it supports commands and aliases). It also
    transparently supports redirection of the standard input and output
    streams using |, >, <, >>, << Ruby operators.

    Working with Shell is self-explanatory most of the time. Take a look
    at the following straightforward examples.

    Creating a Shell object and changing the current directory

    sh = Shell.new
    sh.cd '~/tmp'
    # or
    sh = Shell.cd('~/tmp')
    

    Working within the current directory

    puts "Working directory: #{sh.pwd}"
    (sh.echo 'test') | (sh.tee 'test') > STDOUT
    # Redirecting possible to "left" as well as to "right".
    (sh.cat < 'test') > 'triple_test'
    # '>>' and '<<' are also supported.
    sh.cat('test', 'test') >> 'triple_test'
    

    Note that parentheses are necessary sometimes because of the
    precedence of the redirection operators. Further, the output
    of a command is not printed to by default, so you need to specify
    that use > STDOUT, or > STDERR if needed.

    Testing file properties

    puts sh.test('e', 'test')
    # or
    puts sh[:e, 'test']
    puts sh[:exists?, 'test']
    puts sh[:exists?, 'nonexistent']
    

    Works similar to test function in a usual shell.

    Defining custom commands and aliases

    #                        name    command line to execute
    Shell.def_system_command 'list', 'ls -1'
    
    #                   name   cmd   command's arguments
    Shell.alias_command "lsC", "ls", "-CBF"
    Shell.alias_command("lsC", "ls") { |*opts| ["-CBF", *opts] }
    

    The name of a defined command can be used to run it later (in exactly
    the same way as it works with predefined echo or cat, for example).

    Using the directory stack

    sh.pushd '/tmp'
    sh.list > STDOUT
    (sh.echo sh.pwd) > STDOUT
    sh.popd
    sh.list > STDOUT
    (sh.echo sh.pwd) > STDOUT
    

    Here the custom list command defined above is used.

    By the way, there is a convenient chdir command to run several
    commands in a directory and return to previous working directory
    after that.

    puts sh.pwd
    sh.chdir('/tmp') do
        puts sh.pwd
    end
    puts sh.pwd
    

    Skip the shell object for a group of commands

    # Code above, rewritten to drop out 'sh.' in front of each command.
    sh.transact do
        pushd '/tmp'
        list > STDOUT
        (echo pwd) > STDOUT
        popd
        list > STDOUT
        (echo pwd) > STDOUT
    end
    

    Additional features

    In addition, the Shell class has:

    • the foreach method to iterate through lines in a file, or through
      list of files in a directory (depending on whether given path points
      to a file or a directory);
    • the jobs and kill commands to control processes;
    • a bunch of commands for manipulating files, such as basename,
      chmod, chown, delete, dirname, rename, stat, and symlink;
    • a number of File methods’ synonyms: directory?, executable?,
      exists?, readable?, etc.;
    • the equivalents to the FileTest class methods: syscopy, copy,
      move, compare, safe_unlink, makedirs, and install.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 421k
  • Answers 421k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Why don't you pass the parameters to sp_executeSQL instead? I'd… May 15, 2026 at 11:04 am
  • Editorial Team
    Editorial Team added an answer There doesn't seem to be a way. Erica Sadun has… May 15, 2026 at 11:04 am
  • Editorial Team
    Editorial Team added an answer Verify that you have the right build of Python for… May 15, 2026 at 11:04 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.