Home > Back-end >  Building a simple command to traverse X steps toward the root
Building a simple command to traverse X steps toward the root

Time:06-22

In windows, I had a batch simple script in my path that I could type "up 3" and it would call "cd .." 3 times.

"up.bat"

@echo off                         
if (%1)==() (                     
    cd ..                         
) else (                          
    for /L %%i in (1,1,%1) do (   
        cd ..                     
    )                             
)  

How do I do the same in linux/bash scripting? The below doesn't lead to the directory changing

#!/bin/bash
for i in $(eval echo {1..$1})
do
   cd ..
done

CodePudding user response:

Duplicate of the below, which includes both how to enact on non subshell and also how to make it a raw command

https://unix.stackexchange.com/questions/27139/script-to-change-current-directory-cd-pwd

CodePudding user response:

#!/usr/bin/env bash

for (( i=0; i<$1; i   ))
do
    cd ..
done
$SHELL
  • Related