<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Halonix.dev]]></title><description><![CDATA[Halonix.dev]]></description><link>https://halonix.dev</link><generator>RSS for Node</generator><lastBuildDate>Fri, 10 Apr 2026 08:11:58 GMT</lastBuildDate><atom:link href="https://halonix.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Build A Linux Shell In C++ (With Code)]]></title><description><![CDATA[If you have been using a computer for a while you have probably seen a terminal or maybe even used one. A terminal is a window where a user can interact with the computer through text commands. It is also called a Command Line Interface (CLI) differe...]]></description><link>https://halonix.dev/linux-shell-in-c</link><guid isPermaLink="true">https://halonix.dev/linux-shell-in-c</guid><category><![CDATA[Programming Blogs]]></category><category><![CDATA[System Programming]]></category><category><![CDATA[shell]]></category><category><![CDATA[C++]]></category><dc:creator><![CDATA[Halonix]]></dc:creator><pubDate>Fri, 22 Aug 2025 19:14:19 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1755890442102/8c0f3881-d32a-44c2-b5af-71a66b11438c.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you have been using a computer for a while you have probably seen a terminal or maybe even used one. A terminal is a window where a user can interact with the computer through text commands. It is also called a Command Line Interface (CLI) different than the Graphical User Interface (GUI) . The underlying program that allows the communication between the user and the OS is called the shell.<br />Command Prompt, powershell, Bash are different shells with different features.<br />In this tutorial, we will dive deep into how a shell works under the hood and make our own version of a shell. We’ll use C++ with some POSIX system calls (common in Linux/Unix environments).</p>
<p>Windows shells like Command Prompt or PowerShell don’t use POSIX calls like <code>fork</code>/<code>execvp</code>, but the concept of parsing and executing commands remains the same</p>
<h3 id="heading-prerequisites">Prerequisites:</h3>
<p>Basic knowledge of C++ is required.</p>
<h2 id="heading-what-can-a-shell-do">What can a shell do?</h2>
<p>To build a shell, we need to understand what a shell does. The following are some well known features of a minimal shell:</p>
<ul>
<li><p>List all the files in the current folder.</p>
</li>
<li><p>Travel between directories.</p>
</li>
<li><p>Create a new folder.</p>
</li>
<li><p>Remove a folder.</p>
</li>
<li><p>Create new files.</p>
</li>
</ul>
<p>So our task is to somehow get commands from the user and then relay them to the OS so it can execute them.</p>
<h2 id="heading-build-procedure">Build Procedure:</h2>
<p>The development process will look something like this:</p>
<ol>
<li><p>Get the command from the user in the form of a string e.g. <code>ls</code>, <code>mkdir</code> etc.</p>
</li>
<li><p>Split (parse) the command into separate words. “<code>mkdir newfolder</code>” becomes [mkdir,newfolder].</p>
</li>
<li><p>Create a new process for the task.</p>
</li>
<li><p>Pass the set of words (Tokens) to the OS so it can run.</p>
</li>
</ol>
<h2 id="heading-1-getting-user-input">1. Getting user input:</h2>
<p>First of all, open your IDE of choice and make a new <code>.cpp</code> file.<br />To simulate the shell, we will add a symbol to indicate that the our program is ready to receive input.</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;iostream&gt;</span></span>

<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span> :: <span class="hljs-built_in">cout</span>;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>) {
        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"-&gt;"</span> ;

    }
}
</code></pre>
<p>The ‘→‘ symbol will be an indicator to the user that they should give an input. Next we will take an input in a string.</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;string&gt;</span></span>

<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span> :: <span class="hljs-built_in">cout</span>;
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span> :: <span class="hljs-built_in">string</span>;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-built_in">string</span> command=<span class="hljs-string">""</span>;    
    <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>) {
        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"-&gt;"</span> ;
        <span class="hljs-built_in">std</span> :: getline(<span class="hljs-built_in">cin</span>,command); <span class="hljs-comment">//Getline is used to capture multiple words with spaces</span>
        <span class="hljs-keyword">if</span> ( command.empty() ) <span class="hljs-keyword">continue</span>;<span class="hljs-comment">// In case nothing is entered</span>

    }
}
</code></pre>
<p>This program will now continuously prompt the user to enter a command until a command is entered.</p>
<h2 id="heading-2-splitting-the-input-parsing">2. Splitting the Input (Parsing):</h2>
<p>Now that we have a string “command“ with some command inside. We will split it into tokens. We will write a function which takes in the input string “command“ and returns a container of tokens.</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;string&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;vector&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;sstream&gt;</span></span>

<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span> :: <span class="hljs-built_in">cout</span>;
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span> :: <span class="hljs-built_in">string</span>;

<span class="hljs-function"><span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-built_in">string</span>&gt; <span class="hljs-title">parser</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-built_in">string</span>&amp; input )</span></span>{
    <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-built_in">string</span>&gt; args;<span class="hljs-comment">// make a container that contains strings</span>
    <span class="hljs-function"><span class="hljs-built_in">std</span>::<span class="hljs-built_in">istringstream</span> <span class="hljs-title">iss</span> <span class="hljs-params">(input)</span></span>;<span class="hljs-comment">// split the string word by word</span>
    <span class="hljs-built_in">string</span> token;
    <span class="hljs-keyword">while</span> (iss&gt;&gt;token) { <span class="hljs-comment">// while the string has remaining words</span>
        args.push_back(token);<span class="hljs-comment">// add each token to the vector</span>
    }
    <span class="hljs-keyword">return</span> args;
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-built_in">string</span> command=<span class="hljs-string">""</span>;    
    <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>) {
        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"-&gt;"</span> ;
        <span class="hljs-built_in">std</span> :: getline(<span class="hljs-built_in">cin</span>,command); 
        <span class="hljs-keyword">if</span> ( command.empty() ) <span class="hljs-keyword">continue</span>;
        <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-built_in">string</span>&gt;parsed=parser(command);<span class="hljs-comment">// The vector full of tokens is returned </span>
        <span class="hljs-keyword">if</span>(parsed.empty()) <span class="hljs-keyword">continue</span>;
        <span class="hljs-keyword">if</span> (parsed[<span class="hljs-number">0</span>]==<span class="hljs-string">"quit"</span>|| parsed[<span class="hljs-number">0</span>]==<span class="hljs-string">"exit"</span>) {
            <span class="hljs-keyword">break</span>;
        }
    }
}
</code></pre>
<p>The parser might seem a little new but all it really does is take a string , separates words if they have spaces between them and add each to a vector container. Then the vector is returned .<br />We check the if the first word is empty. We also check if the first word is <code>exit</code> or <code>quit</code> , then the program exits.</p>
<h2 id="heading-3-make-a-vector-of-c-type-strings">3. Make a vector of C-type strings:</h2>
<p>This step is the heart of our shell. We will use an inbuilt function called <code>execvp</code> from the <code>unistd.h</code> header library. However, we cannot pass the vector of strings directly as <code>execvp</code> is a function from the C programming language (where strings don’t exist). So we need to convert it into a vector of C-type strings first.</p>
<h3 id="heading-what-are-c-type-strings">What are C-type strings:</h3>
<p>In the C programming language, a ”string” can be written as an array of characters ( char[] ). A C-type string is a pointer that points to the memory address of the first character of the “string“. The “string“ ends with a ‘ \0 ‘ indicating the end of the string.</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;string&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;vector&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;sstream&gt;</span></span>

<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span> :: <span class="hljs-built_in">cout</span>;
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span> :: <span class="hljs-built_in">string</span>;

<span class="hljs-function"><span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-built_in">string</span>&gt; <span class="hljs-title">parser</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-built_in">string</span>&amp; input )</span></span>{
    <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-built_in">string</span>&gt; args;
    <span class="hljs-function"><span class="hljs-built_in">std</span>::<span class="hljs-built_in">istringstream</span> <span class="hljs-title">iss</span> <span class="hljs-params">(input)</span></span>;
    <span class="hljs-built_in">string</span> token;
    <span class="hljs-keyword">while</span> (iss&gt;&gt;token) { 
        args.push_back(token);
    }
    <span class="hljs-keyword">return</span> args;
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-built_in">string</span> command=<span class="hljs-string">""</span>;    
    <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>) {
        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"-&gt;"</span> ;
        <span class="hljs-built_in">std</span> :: getline(<span class="hljs-built_in">cin</span>,command); 
        <span class="hljs-keyword">if</span> ( command.empty() ) <span class="hljs-keyword">continue</span>;
        <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-built_in">string</span>&gt;parsed=parser(command);
        <span class="hljs-keyword">if</span>(parsed.empty()) <span class="hljs-keyword">continue</span>;
        <span class="hljs-keyword">if</span> (parsed[<span class="hljs-number">0</span>]==<span class="hljs-string">"quit"</span>|| parsed[<span class="hljs-number">0</span>]==<span class="hljs-string">"exit"</span>) {
            <span class="hljs-keyword">break</span>;
        }

        <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">char</span>*&gt; c_strs; <span class="hljs-comment">// create a vector of C-type strings</span>
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> &amp;it:parsed){<span class="hljs-comment">// iterate the vector of tokens</span>
            c_strs.push_back(<span class="hljs-keyword">const_cast</span>&lt;<span class="hljs-keyword">char</span>*&gt;(it.data()));<span class="hljs-comment">// cast the token into char pointer</span>
        }
        c_strs.push_back(<span class="hljs-literal">nullptr</span>);<span class="hljs-comment">// add the null terminating character</span>
    }
}
</code></pre>
<p>Now that we have a vector of C-type strings , we can simply create a new process and execute the command using <code>execvp</code>.</p>
<h2 id="heading-4-execute-the-command">4. Execute the command:</h2>
<p>Now that we have a vector of C-type strings. We can go ahead and pass it through <code>execvp</code>. But we first have to create a process for the task. We can do so using the <code>fork</code> command found in the <code>unistd.h</code> header library. Using <code>pid_t pid=fork()</code>. We can create a new process. That new process is called the child process and the old one is called the parent process.</p>
<p><code>fork</code> creates two paths and returns 0 to indicate the child and a non-zero to indicate the parent. The parent receives the process ID of the child.</p>
<p>Next, We check for the return value of <code>fork</code>, a value of zero means that we are in the child process and we can just execute the command. If the value is greater than zero, that means we are in the parent process and we tell the parent to “wait“ using the function <code>wait</code> present in the <code>sys/wait.h</code> header library.<br />In case the process id is below zero, that means that the <code>fork</code> failed and we should exit.</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;string&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;vector&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;sstream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;unistd.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;sys/wait.h&gt;</span></span>

<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span> :: <span class="hljs-built_in">cout</span>;
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span> :: <span class="hljs-built_in">string</span>;

<span class="hljs-function"><span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-built_in">string</span>&gt; <span class="hljs-title">parser</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-built_in">string</span>&amp; input )</span></span>{
    <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-built_in">string</span>&gt; args;
    <span class="hljs-function"><span class="hljs-built_in">std</span>::<span class="hljs-built_in">istringstream</span> <span class="hljs-title">iss</span> <span class="hljs-params">(input)</span></span>;
    <span class="hljs-built_in">string</span> token;
    <span class="hljs-keyword">while</span> (iss&gt;&gt;token) { 
        args.push_back(token);
    }
    <span class="hljs-keyword">return</span> args;
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-built_in">string</span> command=<span class="hljs-string">""</span>;    
    <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>) {
        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"-&gt;"</span> ;
        <span class="hljs-built_in">std</span> :: getline(<span class="hljs-built_in">cin</span>,command); 
        <span class="hljs-keyword">if</span> ( command.empty() ) <span class="hljs-keyword">continue</span>;
        <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-built_in">string</span>&gt;parsed=parser(command);
        <span class="hljs-keyword">if</span>(parsed.empty()) <span class="hljs-keyword">continue</span>;
        <span class="hljs-keyword">if</span> (parsed[<span class="hljs-number">0</span>]==<span class="hljs-string">"quit"</span>|| parsed[<span class="hljs-number">0</span>]==<span class="hljs-string">"exit"</span>) {
            <span class="hljs-keyword">break</span>;
        }

        <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">char</span>*&gt; c_strs; 
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> &amp;it:parsed){
            c_strs.push_back(<span class="hljs-keyword">const_cast</span>&lt;<span class="hljs-keyword">char</span>*&gt;(it.data()));
        }
        c_strs.push_back(<span class="hljs-literal">nullptr</span>);

        <span class="hljs-keyword">pid_t</span> pId=fork(); <span class="hljs-comment">// create a child process</span>
        <span class="hljs-keyword">if</span> (pId==<span class="hljs-number">0</span>) {<span class="hljs-comment">// if in child process</span>
            execvp(c_strs[<span class="hljs-number">0</span>],c_strs.data()); <span class="hljs-comment">// execute the command i.e vector of C-type strings</span>
            perror(<span class="hljs-string">"Failed"</span>);<span class="hljs-comment">// in case execvp() fails , print failed  </span>
            <span class="hljs-built_in">exit</span>(<span class="hljs-number">1</span>);<span class="hljs-comment">// exit</span>
        }
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (pId&gt;<span class="hljs-number">0</span>) {<span class="hljs-comment">// if in parent </span>
            wait(<span class="hljs-literal">nullptr</span>);<span class="hljs-comment">// wait for child process to terminate</span>
        }
        <span class="hljs-keyword">else</span> {<span class="hljs-comment">// In case pid is below zero</span>
            perror(<span class="hljs-string">"Failed"</span>);
            <span class="hljs-built_in">exit</span>(<span class="hljs-number">1</span>);
        }
}
</code></pre>
<p>Our basic shell is complete. But if you run it, you will notice that it cannot run “cd“ which is the command to change directory. That is because changing the directory only affects the child process, not the parent shell. Once the child exits, the parent’s working directory remains unchanged.<br />So we are going to add a bit of code to ensure support for cd.</p>
<p>If the first token is cd, then we get the location of the directory using <code>getenv</code>. If a location is not provided , the directory location will be <code>home</code>. Then we run <code>chdir</code> to that directory to change the current directory.</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;string&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;vector&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;unistd.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;sstream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;sys/wait.h&gt;</span></span>

<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cin</span>;
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">string</span>;

<span class="hljs-function"><span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-built_in">string</span>&gt; <span class="hljs-title">parser</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-built_in">string</span>&amp; command)</span> </span>{
    <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-built_in">string</span>&gt; args;
    <span class="hljs-function"><span class="hljs-built_in">std</span>::<span class="hljs-built_in">istringstream</span> <span class="hljs-title">iss</span><span class="hljs-params">(command)</span></span>;
    <span class="hljs-built_in">string</span> token;
    <span class="hljs-keyword">while</span> (iss&gt;&gt;token) {
        args.push_back(token);
    }
    <span class="hljs-keyword">return</span> args;
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-built_in">string</span> command;
    <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>) {
        <span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">"-&gt;"</span>;
        <span class="hljs-built_in">std</span>::getline(<span class="hljs-built_in">cin</span>,command);
        <span class="hljs-keyword">if</span> (command.empty()) {
            <span class="hljs-keyword">continue</span>;
        }
        <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-built_in">string</span>&gt;parsed=parser(command);
        <span class="hljs-keyword">if</span>(parsed.empty()) <span class="hljs-keyword">continue</span>;
        <span class="hljs-keyword">if</span> (parsed[<span class="hljs-number">0</span>]==<span class="hljs-string">"quit"</span>|| parsed[<span class="hljs-number">0</span>]==<span class="hljs-string">"exit"</span>) {
            <span class="hljs-keyword">break</span>;
        }
        <span class="hljs-keyword">if</span> (parsed[<span class="hljs-number">0</span>]==<span class="hljs-string">"cd"</span>) { <span class="hljs-comment">// if first token is "cd"</span>
            <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span>* dir=(parsed.size()&gt;<span class="hljs-number">1</span>)?(parsed[<span class="hljs-number">1</span>].c_str()):(getenv(<span class="hljs-string">"HOME"</span>)); <span class="hljs-comment">// get the directory if provided</span>
            <span class="hljs-keyword">if</span> (chdir(dir)!=<span class="hljs-number">0</span>) { <span class="hljs-comment">// change the directory. </span>
                perror(<span class="hljs-string">"cd Failed"</span>); <span class="hljs-comment">//if return code is non-zero , print "cd failed" . </span>
            }
            <span class="hljs-keyword">continue</span>;
        }
        <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">char</span>*&gt; c_strs;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> &amp;it:parsed){
            c_strs.push_back(<span class="hljs-keyword">const_cast</span>&lt;<span class="hljs-keyword">char</span>*&gt;(it.data()));
        }
        c_strs.push_back(<span class="hljs-literal">nullptr</span>);
        <span class="hljs-keyword">pid_t</span> pId=fork();
        <span class="hljs-keyword">if</span> (pId==<span class="hljs-number">0</span>) {
            execvp(c_strs[<span class="hljs-number">0</span>],c_strs.data());
            perror(<span class="hljs-string">"Failed"</span>);
            <span class="hljs-built_in">exit</span>(<span class="hljs-number">1</span>);
        }
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (pId&gt;<span class="hljs-number">0</span>) {
            wait(<span class="hljs-literal">nullptr</span>);
        }
        <span class="hljs-keyword">else</span> {
            perror(<span class="hljs-string">"Failed"</span>);
            <span class="hljs-built_in">exit</span>(<span class="hljs-number">1</span>);
        }
    }
}
</code></pre>
<p>Our shell is now complete. It supports basic command execution, <code>cd</code>, and exiting. If you’ve followed along, you’ve just built a working mini Unix shell and learned about process creation, execution, and even system calls like <code>fork</code>, <code>execvp</code>, <code>wait</code>, and <code>chdir</code>. That’s a huge step into the world of system-level programming!</p>
<p>Below is a demo of the project in action:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1755888985887/fe5cdebb-c4d5-4d09-893d-1c0d9b6cfc2b.gif" alt class="image--center mx-auto" /></p>
<p>The github repository link to the code: <a target="_blank" href="https://github.com/Halonixthefirst/Simple-Unix-Shell">Simple-Unix-Shell</a> .</p>
]]></content:encoded></item></channel></rss>